Send emails with dynamic HTML content using FreeMarker template engine

Photo by HalGatewood.com on Unsplash

Introduction

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. …


Speed up your development and get rid of boilerplate code

Lombok, Indonesia Photo by Tiket2 Fly on Unsplash

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.

Introduction

Lombok (apart from being an island in Indonesia) and MapStruct are code generator libraries


Validate Input in Spring Boot

Photo by M Zefzaf on Unsplash

Introduction

When I was first starting out my career as a developer, I often saw annotations @Validated and @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.

Bean Validation with Spring

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…


Implement search and pagination in a Spring Boot app using MongoTemplate

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 find, findAndModify, findAndReplace, findOne, insert, remove, save, update etc… MongoOperations has fluent APIs for Query and 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.

application.properties file

After that…


Publish and consume messages with Spring Cloud Stream and Apache Kafka message broker

Statue of Franz Kafka, Photo by Sandro Gonzalez on Unsplash

Introduction

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.

Jay Kreps chose to…


Transparently keep track of who created or changed an entity and when the change happened

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.

Annotation-based Auditing Metadata

Spring Data provides several annotations for auditing:

  • @CreatedBy and @LastModifiedBy to capture the user who created or modified the entity.
  • @CreatedDate and @LastModifiedDate to capture when the change happened.

Auditing metadata does not necessarily need to be…


Configure automatic rollback for checked exceptions

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.

Milena Lazarevic

Spring Developer | How-To & Guides Writer | Women In Tech Advocate | Master Troll | LinkedIn https://www.linkedin.com/in/milena-lazarevic/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store