In programming, events are actions that are handled by the software. Events can be triggered in different ways, for example a user clicking a button or a system itself, could trigger an event. The main point is, application needs to listen for such activities, or a particular event, and then react to it and somehow handle it.
In the Spring Framework, one of the central interfaces is
ApplicationContext, and it has an ability to publish events to registered listeners. …
If you are working as a developer, you are already aware that speed is increasingly important. You have deadlines, sprints need to be finished, requirements change and you are under pressure to produce quality code as fast as possible. In that case, this article might not be for you, since you probably already know everything about MapStruct and Lombok.
But, if you are still learning how to code, then this article will teach you the basics of these libraries and how to set them all up.
Lombok (apart from being an island in Indonesia) and MapStruct are code generator libraries…
When I was first starting out my career as a developer, I often saw annotations
@Valid applied on the method parameters in controllers. Some people used
@Validated, while others preferred
@Valid. I was often confused which annotation to use and when. In this article I hope to explain the difference between them once and for all, as best as I can.
Java beans are validated using the standard framework JSR 380, also known as Bean Validation 2.0. JSR 380 is a specification of the Java API for bean validation.
The Spring Framework provides support for the Java Bean…
MongoTemplate class provides us features for interacting with the database and offers operations to create, update, delete, and query MongoDB documents in a thread-safe way. The
MongoTemplate class implements the interface
MongoOperations. You can find methods like
MongoOperations has fluent APIs for
Criteria which we will use in this example.
First, I went on and added connection details to the
application.properties file. Spring Boot will automatically detect this configuration, make a connection to the database and as well create
MongoTemplate instance for us to inject.
Event driven architecture requires forethought, creating the scaffolding in order to integrate popular event streaming platforms can become complicated. Spring Cloud Stream is a framework for message-driven microservice applications and it provides binder implementations for various message brokers, RabbitMQ, Apache Kafka, Kafka Streams, Amazon Kinesis, etc. The framework can simplify things and allow us to easily build message publication and consumption for different platforms, by keeping the specific implementation details of the chosen platform away and by using already familiar Spring idioms and Spring interfaces.
In this post we will be using Apache Kafka message broker.
Spring Data provides support to transparently keep track of who created or changed an entity and when the change happened. The auditing facility allows administrators and users to track system activity with multiple users and applications. To benefit from that functionality, you have to equip your entity classes with auditing metadata that can be defined either using annotations or by implementing an interface.
Spring Data provides several annotations for auditing:
@LastModifiedByto capture the user who created or modified the entity.
@LastModifiedDateto capture when the change happened.
Auditing metadata does not necessarily need to be…
Recently, while I was working on a project I had a strange “bug” (what I thought at first) happen. Apparently, one of my transactions did not automatically rollback after exception was thrown. My whole point of even using @Transactional was to exploit that lovely benefit of having things rollbacked automatically if something went wrong. I never worried much about it… So I, naturally, investigated!
Basically, I discovered that rollback will only happen if the exception being thrown is an unchecked exception, so only runtime exceptions will cause this to happen automatically.
For any checked exception, you have to configure it.