Java · Spring · Thymeleaf · Htmx
Production-ready Spring Boot, crafted carefully.
I'm Wim Deblauwe, a software developer specialized in Java and Spring. I write books, maintain open-source projects like error-handling-spring-boot-starter, and publish practical, battle-tested articles here.
just start typing — try thymeleaf, testcontainers or htmx
Taming Thymeleaf
Server-side rendering with Spring Boot, step by step.
Modern frontends with htmx
Hypermedia-driven UIs with Spring Boot & Thymeleaf.
Recent posts
showing 10 of 136- Aug 30, 2018 Tip on migration to Spring Boot 2 when using Flyway Spring Boot 1.5.x uses Flyway 3 while Spring Boot 2.x uses Flyway 5. According to the official migration guide for Spring Boot 2, you need to do 2 releases to be able to migrate an application from Spring Boot 1 to Spring Boot 2 if you are using Flyway: First upgrade your 1.5.x Spring Boot application to Flyway 4 (4.2.0 at the time of writing), see the instructions for Maven and Gradle. Once your schema has been upgraded to Flyway 4, upgrade to Spring Boot 2 and run the migration again to port your application to Flyway 5.
- Jul 6, 2018 Datadog integration with AWS Elastic Beanstalk for Spring Boot application If you have an application deployed on AWS Elastic Beanstalk and you want to add Datadog integration to it, then these instructions might help you out. I started out with the instructions at https://www.datadoghq.com/blog/deploy-datadog-aws-elastic-beanstalk/ but they use the older version 5 of the datadog-agent and not the newer version 6. In your source code, you normally have a .ebextensions folder to configure the Elastic Beanstalk deployment. Inside that folder, create the following files (Create directories as needed):
- Jun 10, 2018 PageImpl JSON serialization with Spring Boot 2 During the migration of a Spring Boot application from 1.5.13 to 2.0.2, some of my Spring Rest Docs tests started failing. The reason being that some of my RestController s return a org.springframework.data.domain.PageImpl object (from Spring Data Commons). @RestController public class MyRestController { @GetMapping public PageImpl<SomeDtoObject> listAll() { ... } } With Spring Boot 1.5.13, this results in JSON like this: { "content": [ { ... }, { ... }, .... ], "totalElements": 50, "totalPages": 3, "last": false, "size": 20, "number": 0, "numberOfElements": 1, "first": true, "sort": [ { "direction": "DESC", "property": "date", "ignoreCase": false, "nullHandling": "NATIVE", "ascending": false, "descending": true } ] }
- Oct 8, 2017 Building an AsciidoctorJ extension to execute JavaScript I love using Asciidoctor for writing documentation. I mainly got to know it through the excellent Spring REST Docs project. I wanted to build an extension (kind of a plug-in) for Asciidoctor. As I don’t know Ruby, writing an extension in Ruby was a bit too much. Luckily, there is AsciidoctorJ (The JVM version of Asciidoctor) which lets me write extensions in any JVM language. Here, we will be using plain Java, but Groovy for example would work equally well.
- Sep 20, 2017 Using Spring Boot with JavaFX - Using Spring Profiles In the previous post, I showed ./blog/2017/09/18/using-spring-boot-with-javafx/[how to get started with Spring Boot and JavaFX]. Continuing with the same example, we are going to create a new implementation of the CountryService interface that talks to the Open AQ Platform API so the list of countries will be a lot bigger than our HardcodedListCountryService we have now. Using Retrofit First, we add 2 dependencies in our pom.xml to be able to use Retrofit: <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-jackson</artifactId> <version>2.3.0</version> </dependency> Using Retrofit, we can easily get information from the API and transform it using Jackson into Java Objects. First, we create an interface with a method for each call to the API we want to do: public interface OpenAqApi { @GET("countries") Call<OpenAqCountriesResponse> listCountries(); } Next, we create objects that correspond to the returned JSON structures: @Getter @Setter @JsonIgnoreProperties(ignoreUnknown = true) public class OpenAqCountriesResponse { private List<OpenAqCountry> results; } and @Getter @Setter @JsonIgnoreProperties(ignoreUnknown = true) public class OpenAqCountry { private String code; private String name; } Note: We use @JsonIgnoreProperties(ignoreUnknown = true) to avoid having to add properties in Java for JSON fields we are not interested in anyway. Finally, we create an OpenAqServiceImpl class that sets up Retrofit for our API: @Component public class OpenAqServiceImpl implements OpenAqService { private final OpenAqApi api; public OpenAqServiceImpl() { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(interceptor).build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.openaq.org/v1/") .addConverterFactory(JacksonConverterFactory.create()) .client(client) .build(); api = retrofit.create(OpenAqApi.class); } @Override public Set<OpenAqCountry> listCountries() { try { OpenAqCountriesResponse response = api.listCountries().execute().body(); if (response != null) { return new HashSet<>(response.getResults()); } else { return Collections.emptySet(); } } catch (IOException e) { throw new RuntimeException(e); } } } Note: The current code also logs the HTTP requests and responses. For this to work, you need an additional dependency: <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>logging-interceptor</artifactId> <version>3.8.0</version> </dependency> If you don’t want/need this logging, you can remove the dependency. With all this plumbing in place, we can create our actual CountryServiceImpl that is quite simple: @Component public class CountryServiceImpl implements CountryService { private final OpenAqService service; public CountryServiceImpl(OpenAqService service) { this.service = service; } @Override public Set<Country> getAllCountries() { return service.listCountries().stream() .map(openAqCountry -> new Country(openAqCountry.getCode(), openAqCountry.getName())) .collect(Collectors.toSet()); } }
- Sep 18, 2017 Using Spring Boot with JavaFX In my previous post, I showed how to use JavaFX with the Raspberry Pi 3. I promised there to follow up with a post on using Spring Boot with JavaFX, so here it goes. Just as an example, I will be using the REST API service of the Open AQ Platform API as it is simple and does not require any authentication to get started. Initial Setup We will be using Maven, so we start with a minimal pom.xml that includes the latest Spring Boot version (1.5.7 at the time of writing): <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.deblauwe</groupId> <artifactId>airquality-fx</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>AirQualityFX</name> <description>JavaFX Client for the Open AQ Platform API</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> Our minimal JavaFX application will have 1 FXML file that defines the user interface and 1 Java file to get the application running. This is main.fxml: <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Label?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane xmlns="http://javafx.com/javafx"> <Label text="Main Content"/> </AnchorPane> And this is AirQualityFxApplication.java: package org.deblauwe.airqualityfx; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.geometry.Rectangle2D; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Screen; import javafx.stage.Stage; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class AirQualityFxApplication extends Application { private ConfigurableApplicationContext context; private Parent rootNode; @Override public void init() throws Exception { SpringApplicationBuilder builder = new SpringApplicationBuilder(AirQualityFxApplication.class); context = builder.run(getParameters().getRaw().toArray(new String[0])); FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml")); loader.setControllerFactory(context::getBean); rootNode = loader.load(); } @Override public void start(Stage primaryStage) throws Exception { Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds(); double width = visualBounds.getWidth(); double height = visualBounds.getHeight(); primaryStage.setScene(new Scene(rootNode, width, height)); primaryStage.centerOnScreen(); primaryStage.show(); } @Override public void stop() throws Exception { context.close(); } } Some things to note: The class is annotated with @SpringBootApplication to enable Spring Boot We create the Spring ApplicationContext in the init() method We instruct the FXMLLoader to use the context::getBean method to load controllers. This will allow us to have Spring create the controllers using component scanning and inject them into our FXML files. Starting the application from IntelliJ IDEA shows the UI:
- Aug 26, 2017 Using JavaFX on Raspberry Pi 3 with Gluon In this post, I am going to detail the steps to get a simple JavaFX application running on the Raspberry Pi 3 using Gluon. Hardware For this setup, I used the Raspberry Pi 3 with the official 7" touchscreen connected. Raspbian Installation First off, download Raspbian from https://www.raspberrypi.org/downloads/raspbian/. I chose the "RASPBIAN STRETCH WITH DESKTOP" version and burned it on an SD-card using Etcher. Insert the SD-card in the Raspberry Pi and let it boot. The screen should look like this when done: Find out the IP address of the Raspberry Pi by running ifconfig on the Pi itself or looking at the DHCP clients list of your router. Enable SSH to be able to easily copy files to the Raspberry Pi. See the official instructions on how to do that.
- Jul 24, 2017 Enable preview of Spring REST Docs snippets in IntelliJ IDEA If you use Spring REST Docs, you will be using the Asciidoctor include directive a lot to include the request and responses that are generated from the unit tests. Drawback is that the Asciidoctor plugin for IntelliJ IDEA will not render them by default and looks like this: The reason is simple, the snippets attribute is only available when running the Maven or Gradle task to build the final documentation.
- Mar 13, 2017 Using ServletUriComponentsBuilder with Nginx Spring’s ServletUriComponentsBuilder.fromCurrentRequest() can be really convinient to build an URL from the current request. However, when using Nginx, things might work so well. The solution is simple, just add these headers to your Nginx configuration file: proxy_set_header X-Forwarded-Host $host:443; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-Port 443; proxy_set_header X-Forwarded-Proto https; UPDATE 2020-06-08 You need to enable support for this in Spring Boot by setting the following property (Since Spring Boot 2.2): server.forward-headers-strategy=native
- Jan 22, 2017 AssertJ custom assertion for ConstraintValidator tests As a follow-up to my last post on a custom validator to check if a String contains XML, I like to elaborate on how I made the unit test so readable. To repeat, here is part of the unit test again with the assertions highlighted: @Test public void givenNoXml_notValid() { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); TestObject testObject = new TestObject("This is no XML string"); Set<ConstraintViolation<TestObject>> violationSet = validator.validate(testObject); assertThat(violationSet).hasViolationOnPath("xml"); } @Test public void givenXml_valid() { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); TestObject testObject = new TestObject("<Node>test</Node>"); Set<ConstraintViolation<TestObject>> violationSet = validator.validate(testObject); assertThat(violationSet).hasNoViolations(); } I love using AssertJ for assertions in my unit tests, because: They are very readable They are code completion friendly You don’t have to remember if the expected value goes first or last
no matches for "" — browse the full archive →