Why does Spring MVC need at least two contexts?

Having a root web application context plus a child servlet context is just an option. If you know that your application won’t have a second servlet, it’s arguably simpler to have one single Spring context for the whole web application. You can achieve that setup by simply removing the ContextLoaderListener (and the accompanying contextConfigLocation context-param) … Read more

Handling ambiguous handler methods mapped in REST application with Spring

Spring can’t distinguish if the request GET http://localhost:8080/api/brand/1 will be handled by getBrand(Integer) or by getBrand(String) because your mapping is ambiguous. Try using a query parameter for the getBrand(String) method. It seems more appropriate, since you are performing a query: @RequestMapping(value = “/{id}”, method = RequestMethod.GET) public Brand getBrand(@PathVariable Integer id) { return brandService.getOne(id); } … Read more

How to handle DataIntegrityViolationException in Spring?

The problem with showing user-friendly messages in the case of constraint violation is that the constraint name is lost when Hibernate’s ConstraintViolationException is being translated into Spring’s DataIntegrityViolationException. However, you can customize this translation logic. If you use LocalSessionFactoryBean to access Hibernate, you can supply it with a custom SQLExceptionTranslator (see LocalSessionFactoryBean.jdbcExceptionTranslator). This exception translator … Read more

Spring: define @RequestMapping value in a properties file

It should be possible to use placeholders in @RequestMapping, like for example @RequestMapping(“${foo.bar}”). Take a look at the documentation for more details: Patterns in @RequestMapping annotations support ${…​} placeholders against local properties and/or system properties and environment variables. This may be useful in cases where the path a controller is mapped to may need to … Read more

Spring injection Into Servlet

I wanted to leverage on the solution provided by Sotirios Delimanolis but adding transparent autowiring to the mix. The idea is to turn plain servlets into autowire-aware objects. So I created a parent abstract servlet class that retrieves the Spring context, gets and autowiring-capable factory and uses that factory to autowire the servlet instances (the … Read more

How to get custom user info from OAuth2 authorization server /user endpoint

The solution is the implementation of a custom UserInfoTokenServices https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServices.java Just Provide your custom implementation as a Bean and it will be used instead of the default one. Inside this UserInfoTokenServices you can build the principal like you want to. This UserInfoTokenServices is used to extract the UserDetails out of the response of the /usersendpoint … Read more

Can I make a custom controller mirror the formatting of Spring-Data-Rest / Spring-Hateoas generated classes?

I’ve found a way to imitate the behavior of Spring Data Rest completely. The trick lies in using a combination of the PagedResourcesAssembler and an argument-injected instance of PersistentEntityResourceAssembler. Simply define your controller as follows… @RepositoryRestController @RequestMapping(“…”) public class ThingController { @Autowired private PagedResourcesAssembler pagedResourcesAssembler; @SuppressWarnings(“unchecked”) // optional – ignores warning on return statement below… … Read more

Spring forward with added parameters?

The simplest way is to add the data to the request. Since this is a forward, the same request is passed around to different handlers within the server. As example, let’s start with a simple setup of two controllers, one forwarding to the other: @Controller public class TestController { @RequestMapping(value=”/test”) public String showTestPage() { return … Read more

spring PropertyPlaceholderConfigurer and context:property-placeholder

<context:property-placeholder … /> is the XML equivalent to the PropertyPlaceholderConfigurer. So, prefer that. The <util:properties/> simply factories a java.util.Properties instance that you can inject. In Spring 3.1 (not 3.0…) you can do something like this: @Configuration @PropertySource(“/foo/bar/services.properties”) public class ServiceConfiguration { @Autowired Environment environment; @Bean public javax.sql.DataSource dataSource( ){ String user = this.environment.getProperty(“ds.user”); … } … Read more