Spring Resttemplate exception handling

You want to create a class that implements ResponseErrorHandler and then use an instance of it to set the error handling of your rest template: public class MyErrorHandler implements ResponseErrorHandler { @Override public void handleError(ClientHttpResponse response) throws IOException { // your error handling here } @Override public boolean hasError(ClientHttpResponse response) throws IOException { … } … Read more

Spring RestTemplate timeout

For Spring Boot >= 1.4 @Configuration public class AppConfig { @Bean public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) { return restTemplateBuilder .setConnectTimeout(…) .setReadTimeout(…) .build(); } } For Spring Boot <= 1.3 @Configuration public class AppConfig { @Bean @ConfigurationProperties(prefix = “custom.rest.connection”) public HttpComponentsClientHttpRequestFactory customHttpRequestFactory() { return new HttpComponentsClientHttpRequestFactory(); } @Bean public RestTemplate customRestTemplate() { return new RestTemplate(customHttpRequestFactory()); } } … Read more

How to define @Value as optional

What is the correct way to specify that @Value is not required? Working on the assumption that by ‘not required’ you mean null then… You have correctly noted that you can supply a default value to the right of a : character. Your example was @Value(“${myValue:DEFAULT}”). You are not limited to plain strings as default … 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 Test & Security: How to mock authentication?

Seaching for answer I couldn’t find any to be easy and flexible at the same time, then I found the Spring Security Reference and I realized there are near to perfect solutions. AOP solutions often are the greatest ones for testing, and Spring provides it with @WithMockUser, @WithUserDetails and @WithSecurityContext, in this artifact: <dependency> <groupId>org.springframework.security</groupId> … 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

How to get access to HTTP header information in Spring MVC REST controller?

When you annotate a parameter with @RequestHeader, the parameter retrieves the header information. So you can just do something like this: @RequestHeader(“Accept”) to get the Accept header. So from the documentation: @RequestMapping(“/displayHeaderInfo.do”) public void displayHeaderInfo(@RequestHeader(“Accept-Encoding”) String encoding, @RequestHeader(“Keep-Alive”) long keepAlive) { } The Accept-Encoding and Keep-Alive header values are provided in the encoding and keepAlive … Read more

Spring Security on Wildfly: error while executing the filter chain

Investigating the problem I have noticed that there is some mess with cookies and referers in the auth request. Currently wildfly authentication will work if you change webapplication context to the Root Context: <server name=”default-server” default-host=”webapp”> <http-listener name=”default” socket-binding=”http”/> <host name=”default-host” alias=”localhost” default-web-module=”sso.war”/> </server> After restarting wildfly and clearing cookies all should work as expected

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