Gradle 5 JUnit BOM and Spring Boot Incorrect Versions

How do I disable JUnit coming from the Gradle Spring Dependency Management plugin? For starters, if you are using the dependency management plugin from Spring, you should not be importing the junit-bom since that results in duplicate (and potentially conflicting) management of those dependencies. Aside from that, whenever you use the dependency management plugin from … Read more

How to access in memory h2 database of one spring boot application from another spring boot application

You can setup H2 Server as Spring Bean. First edit pom.xml – delete <scope>runtime</scope> from h2 dependency: <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> Then add H2 server bean to SpringBootApplication or Configuration class: @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } /** * Start internal H2 server so we can query … Read more

How to log request and response bodies in Spring WebFlux

This is more or less similar to the situation in Spring MVC. In Spring MVC, you can use a AbstractRequestLoggingFilter filter and ContentCachingRequestWrapper and/or ContentCachingResponseWrapper. Many tradeoffs here: if you’d like to access servlet request attributes, you need to actually read and parse the request body logging the request body means buffering the request body, … Read more

Jersey returns 404 with any error status code?

The default behavior with Jersey, when there is an error status (4xx, 5xx), is to use the servlet’s Response.sendError, which results in a redirect to an error page. Since there is no error page set up, it results in a 404. We can change this behavior by setting the Jersey property ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR You can do … Read more

Disable Logback in SpringBoot

Add exclusion to both the spring-boot-starter and spring-boot-starter-web to resolve the conflict. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>

Spring Boot & JPA: Implementing search queries with optional, ranged criteria

You can achieve complex queries with specifications by JpaSpecificationExecutor in spring data. Repository interface must extend the JpaSpecificationExecutor<T> interface so we can specify the conditions of our database queries by creating new Specification<T> objects. The trick is in the use of the Specification interface in combination with a JpaSpecificationExecutor. here is the example: @Entity @Table(name … Read more

JSP file not rendering in Spring Boot web application

Make sure that your pom.xml specifies the Tomcat JSP dependency as follows: <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> It seems that embedded Tomcat treats the JSP rendering as optional. As mentioned below, this JAR is sometimes necessary as well: <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <scope>provided</scope> </dependency> (I added provided since this JAR should be included by the servlet … Read more

Springboot Security hasRole not working

You have to name your authority with prefix ROLE_ to use isUserInRole, see Spring Security Reference: The HttpServletRequest.isUserInRole(String) will determine if SecurityContextHolder.getContext().getAuthentication().getAuthorities() contains a GrantedAuthority with the role passed into isUserInRole(String). Typically users should not pass in the “ROLE_” prefix into this method since it is added automatically. For example, if you want to determine … Read more