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- Jan 21, 2017 Custom Validator to check if a String contains XML This blog post will show you how to create a custom validator to check if a String contains valid XML using the Java Validation API. Suppose you have a highly technical application that requires the user to enter some XML in a web form. You want to validate this on the client side, but also on the server side, since you should never trust your client. For the purpose of the example, suppose you have this entity:
- May 5, 2016 Angular datatables with server side pagination using Spring Data We are using angular-datatables in a project. So far, we just returned all entities from the server’s REST controller (Using Spring Boot and Spring Data on the server side). I wanted to see how I could implement server side pagination to avoid returning all records at once. I was lucky to find spring-data-jpa-datatables which makes it very easy to do. First, add the dependency in your pom.xml: <dependency> <groupId>com.github.darrachequesne</groupId> <artifactId>spring-data-jpa-datatables</artifactId> <version>2.1</version> </dependency> In my UserController, I have currently this:
- Apr 1, 2016 AssertJ custom assertion for testing ConstraintValidator implementations If you want to unit test a ConstraintValidator with AssertJ, then you can use this custom validator to make the tests more readable: import org.assertj.core.api.AbstractAssert; import javax.validation.ConstraintViolation; import java.util.Set; import java.util.stream.Collectors; public class ConstraintViolationSetAssert extends AbstractAssert<ConstraintViolationSetAssert, Set<? extends ConstraintViolation>> { public ConstraintViolationSetAssert(Set<? extends ConstraintViolation> actual) { super(actual, ConstraintViolationSetAssert.class); } public static ConstraintViolationSetAssert assertThat(Set<? extends ConstraintViolation> actual) { return new ConstraintViolationSetAssert(actual); } public ConstraintViolationSetAssert hasViolationOnPath(String path) { isNotNull(); // check condition if (!containsViolationWithPath(actual, path)) { failWithMessage("There was no violation with path <%s>. Violation paths: <%s>", path, actual.stream() .map(violation -> violation .getPropertyPath() .toString()) .collect( Collectors .toList())); } return this; } private boolean containsViolationWithPath(Set<? extends ConstraintViolation> violations, String path) { boolean result = false; for (ConstraintViolation violation : violations) { if (violation.getPropertyPath().toString().equals(path)) { result = true; break; } } return result; } } An example unit test that uses this: @Test public void givenInvalidUsername_violationConstraint() { CreateUserParameters p = new CreateUserParameters(); p.setUsername("x"); ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); Set<ConstraintViolation<CreateUserParameters>> violationSet = validator.validate(parameters); // static import for ConstraintViolationSetAssert assertThat(violationSet).hasViolationOnPath("username"); } See http://joel-costigliola.github.io/assertj/assertj-core-custom-assertions.html for the documentation if you want to create your own custom assertions using AssertJ. There is even a Maven plugin that allows to generate custom assertions automatically: http://joel-costigliola.github.io/assertj/assertj-assertions-generator-maven-plugin.html
- Sep 30, 2015 Read only EntryProcessors with Hazelcast This post is a follow-up on my post EntryProcessors and EntryBackupProcessors with Hazelcast. In the comments, Peter Veentjer from Hazelcast gave me the idea to use an EntryProcessor to read part of the data. I will show you below how to best do this. We start with a cache that has 10 User objects in it and we want to retrieve the user names of all users in the cache. Without an entry processor We can get the list of names without using an entry processor. For example: public class ReadOnlyEntryProcessorTest0 { public static void main(String[] args) throws InterruptedException { Config config = new Config(); config.setProperty("hazelcast.initial.min.cluster.size", "2"); HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config); // Take a lock so only 1 of the 2 nodes will execute the entry processor ILock doItLock = hazelcastInstance.getLock("doItLock"); doItLock.lock(); IMap<Long, User> testMap; try { testMap = hazelcastInstance.getMap("testMap"); if (!testMap.containsKey(1L)) { IntStream.rangeClosed(1, 10) .mapToObj(ReadOnlyEntryProcessorTest0::createUser) .forEach(user -> testMap.put(user.getId(), user)); testMap.values().stream().map(User::getName).forEach(name -> System.out.println("name = " + name)); } } finally { doItLock.unlock(); } } private static User createUser(int i) { User user = new User(); user.setId(i); user.setName("Wim" + i); return user; } } So the example first adds 10 User objects in the cache and then retrieves them again, getting the "name" of each User object and printing this. This means Hazelcast has to fetch (and serialize) the full User object from the other nodes running in the cluster. Since we are only interested in a small part of the object, we can use an EntryProcessor to retrieve just that.
- Sep 29, 2015 EntryProcessors and EntryBackupProcessors with Hazelcast Hazelcast has the concept of EntryProcessors (like Oracle Coherence). EntryProcessors allow to update cache entries without having to pull over the actual values. You move the processing to where the value lives and update the cache there. Furthermore, Hazelcast has the notion of EntryBackupProcessor (which Coherence does not have). To explain the usage of this, we will use a simple User class: class User implements Serializable { private long id; private String name; private DateTime lastLoginTime; // getters and setters omitted } We will set the 'lastLoginTime' on the user by means of an EntryProcessor.
- Mar 24, 2015 Introduction to using JavaFX with afterburner.fx I wanted to try out afterburner.fx, a JavaFX framework which describes itself as: a minimalistic (3 classes) JavaFX MVP framework based on Convention over Configuration and Dependency Injection. For this purpose I created a simple note taking application which looks like this when finished: First off, the domain class that represents a Note: public class Note { private long id; private String title; private String content; // Getter and setters ommitted I also made a NoteService to retrieve the current notes and update an existing note:
- Mar 13, 2015 Using Font Awesome in JavaFX with fontawesomefx Icons are a great way to spice up any UI. You can easily use the Font Awesome icons in JavaFX, by using the fontawesomefx library. I will show a small example on how to use the icons in JavaFX code and how to apply some styling. First import the library. I am using Maven, so I just add this dependency: <dependency> <groupId>de.jensd</groupId> <artifactId>fontawesomefx</artifactId> <version>8.2</version> </dependency> We will start with a simple app that uses a BorderPane to put some content at the center and have a kind of header at the top:
- Nov 4, 2014 Spring Boot application with exploded directory structure Spring Boot is really amazing for getting started quickly with a new Spring application. By default, your application is contained in a single jar when packaging it. This has some advantages, but what if you want a "classic" layout with a config folder (for your application.properties or logback.xml files) and a lib folder? Getting Started This blog post will show you a way of doing this using Maven and the Maven Assembly Plugin. First, we create a simple project using the Spring Initializr. I opted for Java 8 and selected the "Web" dependency. The current Spring Boot version is 1.1.8. This gave me a zip file with the following structure: pom.xml src/main/java/.../Application.java src/main/resources/application.properties src/test/java/... For some fun, I added a simple rest controller: package org.deblauwe.example.boot.exploded; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestController { @Value("${hello.value:World}") private String helloValue; @RequestMapping("/") public String sayHello() { return "Hello " + helloValue; } } Running this application and pointing to http://localhost:8080/test/ returns "Hello World" in the browser. We can now inject a different value into 'helloValue' by adding the following line to application.properties: hello.value=Maven If you now refresh the browser, it shows: Hello Maven
- Jul 16, 2014 Opening multiple SSH sessions with iTerm automatically iTerm is great, but if you are working with a cluster of servers, it quickly becomes tedious to open an SSH session to each server and to configure splits so you can talk to all servers at once. Based upon the scripts here (but does not use splits) and here (but does not work with 7 servers which I needed in my case), I came up with the following AppleScript that works for me.
- Jun 11, 2014 Using Cassandra unit with TestNG I just started some tests with Cassandra and Spring Data Cassandra. I want to write some unit tests for this using TestNG. The Cassandra Unit project uses JUnit by default, but can be used with TestNG as well with some tweaking. To start, add the following dependencies in your Maven pom: <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-cassandra</artifactId> <version>1.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.apache.cassandra</groupId> <artifactId>cassandra-all</artifactId> <version>2.0.5</version> </dependency> <dependency> <groupId>org.cassandraunit</groupId> <artifactId>cassandra-unit</artifactId> <version>2.0.2.1</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>cassandra-all</artifactId> <groupId>org.apache.cassandra</groupId> </exclusion> <exclusion> <groupId>com.datastax.cassandra</groupId> <artifactId>cassandra-driver-core</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.cassandraunit</groupId> <artifactId>cassandra-unit-spring</artifactId> <version>2.0.2.1</version> <scope>test</scope> </dependency> Now we can setup Spring context loading with TestNG (using Java Config): @Test @ContextConfiguration public class CassandraMessageRepositoryTest extends AbstractTestNGSpringContextTests { public void testStoreMessage() { } @Configuration static class ContextConfiguration { // Add your @Bean's here } } After this,we need to start an embedded Cassandra before the Spring context loads, since the spring context needs to connect to the Cassandra server. For this, I have found no better way than overriding the @TestExecutionListeners.
no matches for "" — browse the full archive →