How to shutdown a Spring Boot Application in a correct way?

If you are using the actuator module, you can shutdown the application via JMX or HTTP if the endpoint is enabled. add to application.properties: endpoints.shutdown.enabled=true Following URL will be available: /actuator/shutdown – Allows the application to be gracefully shutdown (not enabled by default). Depending on how an endpoint is exposed, the sensitive parameter may be … Read more

Difference between Interceptor and Filter in Spring MVC

From HandlerIntercepter‘s javadoc: HandlerInterceptor is basically similar to a Servlet Filter, but in contrast to the latter it just allows custom pre-processing with the option of prohibiting the execution of the handler itself, and custom post-processing. Filters are more powerful, for example they allow for exchanging the request and response objects that are handed down … Read more

Spring Boot and multiple external configuration files

UPDATE: As the behaviour of spring.config.location now overrides the default instead of adding to it. You need to use spring.config.additional-location to keep the defaults. This is a change in behaviour from 1.x to 2.x When using Spring Boot the properties are loaded in the following order (see Externalized Configuration in the Spring Boot reference guide). … Read more

Java Spring Boot: How to map my app root (“/”) to index.html?

It would have worked out of the box if you hadn’t used @EnableWebMvc annotation. When you do that you switch off all the things that Spring Boot does for you in WebMvcAutoConfiguration. You could remove that annotation, or you could add back the view controller that you switched off: @Override public void addViewControllers(ViewControllerRegistry registry) { … Read more

Read file from resources folder in Spring Boot

After spending a lot of time trying to resolve this issue, finally found a solution that works. The solution makes use of Spring’s ResourceUtils. Should work for json files as well. Thanks for the well written page by Lokesh Gupta : Blog package utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.ResourceUtils; import java.io.FileInputStream; import java.io.IOException; import … Read more

Disable all Database related auto configuration in Spring Boot

The way I would do similar thing is: @Configuration @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) @Profile (“client_app_profile_name”) public class ClientAppConfiguration { //it can be left blank } Write similar one for the server app (without excludes). Last step is to disable Auto Configuration from main spring boot class: @SpringBootApplication public class SomeApplication extends SpringBootServletInitializer { public … Read more

Could not autowire field:RestTemplate in Spring boot application

It’s exactly what the error says. You didn’t create any RestTemplate bean, so it can’t autowire any. If you need a RestTemplate you’ll have to provide one. For example, add the following to TestMicroServiceApplication.java: @Bean public RestTemplate restTemplate() { return new RestTemplate(); } Since Spring boot 1.4, there’s also a convenient builder that you can … Read more

Spring Boot Remove Whitelabel Error Page

You need to change your code to the following: @RestController public class IndexController implements ErrorController{ private static final String PATH = “/error”; @RequestMapping(value = PATH) public String error() { return “Error handling”; } @Override public String getErrorPath() { return PATH; } } Your code did not work, because Spring Boot automatically registers the BasicErrorController as … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)