How to enable spring support in IntelliJ Community Edition 2016.1.3

You can’t enable Spring support with IntelliJ community Edition, it only available with paying version (Ultimate). However, you can create the .xml file manually and CE version also supports it (a little bit). To working with Spring or J2EE, you should get familiar with build tools like Maven, Gradle (or Ant in some special case). … Read more

Access to h2 web console while running junit test in a Spring application

As this is probably going to be a test-debugging feature, you can add it at runtime with your @Before: import org.h2.tools.Server; /* Initialization logic here */ @BeforeAll public void initTest() throws SQLException { Server.createWebServer(“-web”, “-webAllowOthers”, “-webPort”, “8082”) .start(); } And then connect to http://localhost:8082/ Note: unless you need this to run as part of your … Read more

Get field name when javax.validation.ConstraintViolationException is thrown

If you inspect the return value of getPropertyPath(), you’ll find it’a Iterable<Node> and the last element of the iterator is the field name. The following code works for me: // I only need the first violation ConstraintViolation<?> violation = ex.getConstraintViolations().iterator().next(); // get the last node of the violation String field = null; for (Node node … Read more

Spring Boot project with static content generates 404 when running jar

It turns out that whilst Spring Boot is being clever at adding the various resource directories to the classpath, Maven is not and it’s up to you to deal with that part. By default, only src/main/resources will be included in your JAR. If you create a folder called /static in the root of your project … Read more

Spring Websocket in a tomcat cluster

Horizontally scaling WebSockets is actually very different than horizontally scaling stateless/stateful HTTP only based applications. Horizontally Scaling Stateless HTTP app: just spin up some application instances in different machines and put a load balancer in front of them. There are quite a lot different load balancer solutions such as HAProxy, Nginx, etc. If you are … Read more

Customize spring validation error

The JSR 303 default message interpolation algorithm allows you to customize messages by supplying a resource bundle named ValidationMessages. Create a ValidationMessages.properties file in the classpath containing: javax.validation.constraints.NotNull.message=CUSTOM NOT NULL MESSAGE javax.validation.constraints.Size.message=CUSTOM SIZE MESSAGE This changes the default message for the @Size constraint, so you should use the @Size constraint instead of the Hibernate-specific @Length … Read more

Why use service layer?

Having the service layer be a wrapper around the DAO is a common anti-pattern. In the example you give it is certainly not very useful. Using a service layer means you get several benefits: you get to make a clear distinction between web type activity best done in the controller and generic business logic that … Read more

Proper way of streaming using ResponseEntity and making sure the InputStream gets closed

you can try to use StreamingResponseBody StreamingResponseBody A controller method return value type for asynchronous request processing where the application can write directly to the response OutputStream without holding up the Servlet container thread. Because you are working on a separate thread, writing directly to the response, your problem to call close() before return is … Read more