spring property substitution for test and production

several approaches: 1. ‘Order’ Property in src/main/resources/your-conf.xml <context:property-placeholder location=”classpath:esb-project-config.properties” order=”1″/> in src/test/resources/your-test-config.xml <context:property-placeholder location=”classpath:esb-project-config.properties” order=”0″/> If you running your test with src/test/resources as a test classpath, the above will ensure to override src/main/resources/esb-project-config.properties with the src/test/resources/esb-project-config.properties. This will override the whole property-placeholder though, so you would have to provide all the properties needed in your … Read more

Spring/Rest @PathVariable character encoding

I thing that you need add filter to web.xml <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

Logging response body (HTML) from HttpServletResponse using Spring MVC HandlerInterceptorAdapter

This would be better done using a Servlet Filter rather than a Spring HandlerInterceptor, for the reason that a Filter is allowed to substitute the request and/or response objects, and you could use this mechanism to substitute the response with a wrapper which logs the response output. This would involve writing a subclass of HttpServletResponseWrapper, … Read more

SPRING REST: The request was rejected because no multipart boundary was found

The problem isn’t in your code – it’s in your request. You’re missing boundary in your multipart request. As it said in specification: The Content-Type field for multipart entities requires one parameter, “boundary”, which is used to specify the encapsulation boundary. The encapsulation boundary is defined as a line consisting entirely of two hyphen characters … Read more

Custom Authentication Manager with Spring Security and Java Configuration

Take a look at my sample below. You have to return an UsernamePasswordAuthenticationToken. It contains the principal and the GrantedAuthorities. Hope I could help 🙂 public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = authentication.getPrincipal() + “”; String password = authentication.getCredentials() + “”; User user = userRepo.findOne(username); if (user == null) { throw new … Read more

Can we declare spring bean conditionally?

You can use @Conditional from Spring4 or @ConditionalOnProperty from Spring Boot. Using Spring4 (only) if you are NOT using Spring Boot, this can be overkill. First, create a Condition class, in which the ConditionContext has access to the Environment: public class MyCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment env … Read more

Multiple data source and schema creation in Spring Boot

spring.jpa.hibernate.ddl-auto=create has stopped working, not because you have two DataSources, but because your application’s creating its own LocalContainerEntityManagerFactoryBeans. This has the effect of disabling the auto-configuration of a LocalContainerEntityManagerFactoryBean so you now have to configure it yourself. You can configure the two entity managers to have different schema generation behaviour like this (the first’s doing … Read more