Spring Boot Page Deserialization – PageImpl No constructor

Found the answer in the different post on here. It was not the accepted answer which it should be Spring RestTemplate with paginated API the answer by @rvheddeg is correct. You just need to put @JsonCreator and provide a constructor with all the properties, here is my RestResponsePage class that fixes the issue. class RestResponsePage<T> … Read more

How to apply spring boot filter based on URL pattern?

There is another option if you are able to extend OncePerRequestFilter. For example: public class SomeFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { // your filter logic …. } @Override protected boolean shouldNotFilter(HttpServletRequest request) { String path = request.getServletPath(); return !path.startsWith(“/api/secure/”); } }

Spring Boot validation message is not being resolved

It looks like you are missing LocalValidatorFactoryBean definition in your application configuration. Below you can find an example of Application class that defines two beans: LocalValidatorFactoryBean and MessageSource that uses messages.properties file. import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; @SpringBootApplication public class Application { @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource … Read more

@MAPSTRUCT. No property named “packaging” exists in source parameter(s)

For those, who have the same issue when using mapstruct + lombok: I had the same issue. The reason was that I’ve been using Lombok plugin too. There’s no need to remove it, but you have to ensure that in pom.xml in <annotationProcessorPaths> Lombok tag is before the Mapstruct one. Example (part of pom.xml file): … Read more

Mock external server during integration testing with Spring

After playing a bit with various scenarios, here is the one way how can one achieve what was asked with minimal interventions to the main code Refactor your controller to use a parameter for thirdparty server address: @RestController public class HelloController { @Value(“${api_host}”) private String apiHost; @RequestMapping(“/hello_to_facebook”) public String hello_to_facebook() { // Ask facebook about … Read more

Why to use @AllArgsConstructor and @NoArgsConstructor together over an Entity?

The JPA specification requires that all persistent classes (@Entity) have a no-arg constructor, public or protected. (note that this is not necessarily true when dealing with some implementation like Hibernate, see this answer). This is needed because JPA uses the default constructor method to create a bean class using the reflection API. Indeed if your … Read more

Deploy Spring Boot to Tomcat

The chapter Packaging executable jar and war files in the Spring Boot reference documentation states: To build a war file that is both executable and deployable into an external container you need to mark the embedded container dependencies as “provided”, e.g: <?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”> <!– … –> <packaging>war</packaging> <!– … … Read more

Configure Spring Boot with two ports

As is has been mentioned before, server.port and management.port along with management.context-path properties could be set to make the embedded container to listen on different ports (management-related properties to access Actuator endpoints). To listen on ports other than server.port and management.port: @Configuration public class EmbeddedTomcatConfiguration { @Value(“${server.additionalPorts}”) private String additionalPorts; @Bean public EmbeddedServletContainerFactory servletContainer() { … Read more