ContentCachingResponseWrapper Produces Empty Response

After couple of hours of struggling, I’ve finally found the solution. In short, ContentCachingResponseWrapper.copyBodyToResponse() should be called in the end of the filter method. ContentCachingResponseWrapper caches the response body by reading it from response output stream. So, the stream becomes empty. To write response back to the output stream ContentCachingResponseWrapper.copyBodyToResponse() should be used.

Dynamic Selection Of JsonView in Spring MVC Controller

On the off chance someone else wants to achieve the same thing, it actually is very simple. You can directly return aorg.springframework.http.converter.json.MappingJacksonValue instance from your controller that contains both the object that you want to serialise and the view class. This will be picked up by the org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter#writeInternal method and the appropriate view will be … Read more

How to create a Spring Interceptor for Spring RESTful web services

Following steps can be taken to implement the interceptor with Spring: Implement an interceptor class extending HandlerInterceptorAdapter class. Following is how the code could look like: public class LoginInterceptor extends HandlerInterceptorAdapter { @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception { // TODO Auto-generated method stub } @Override public void … Read more

Spring 5.0.3 RequestRejectedException: The request was rejected because the URL was not normalized

Spring Security Documentation mentions the reason for blocking // in the request. For example, it could contain path-traversal sequences (like /../) or multiple forward slashes (//) which could also cause pattern-matches to fail. Some containers normalize these out before performing the servlet mapping, but others don’t. To protect against issues like these, FilterChainProxy uses an … Read more

Spring mvc @PathVariable

suppose you want to write a url to fetch some order, you can say www.mydomain.com/order/123 where 123 is orderId. So now the url you will use in spring mvc controller would look like /order/{orderId} Now order id can be declared a path variable @RequestMapping(value = ” /order/{orderId}”, method=RequestMethod.GET) public String getOrder(@PathVariable String orderId){ //fetch order … Read more

Difference between Request MVC and Component MVC [closed]

In request (action) based MVC, a single front controller servlet will delegate to action models based on request URL/params. You work directly with raw HttpServletRequest and HttpServletResponse objects in the action model. You’ve to write code yourself to gather, convert and validate the request parameters and if necessary update the model values before you can … Read more

CharacterEncodingFilter don’t work together with Spring Security 3.2.0

We need to add CharacterEncodingFilter before filters who read request properties for the first time. There is securityFilterChain (stands second. after metrica filter) and we can add our filter inside it. The first filter (inside security chain) who reads properties is CsrfFilter, so we place CharacterEncodingFilter before it. The short solution is: @Configuration @EnableWebMvcSecurity public … Read more

Sending Multipart File as POST parameters with RestTemplate requests

A way to solve this without needing to use a FileSystemResource that requires a file on disk, is to use a ByteArrayResource, that way you can send a byte array in your post (this code works with Spring 3.2.3): MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); final String filename=”somefile.txt”; map.add(“name”, filename); map.add(“filename”, filename); ByteArrayResource contentsAsResource … Read more

@ModelAttribute annotation, when to use it?

You don’t need @ModelAttribute (parameter) just to use a Bean as a parameter For example, these handler methods work fine with these requests: @RequestMapping(“/a”) void pathA(SomeBean someBean) { assertEquals(“neil”, someBean.getName()); } GET /a?name=neil @RequestMapping(value=”/a”, method=RequestMethod.POST) void pathAPost(SomeBean someBean) { assertEquals(“neil”, someBean.getName()); } POST /a name=neil Use @ModelAttribute (method) to load default data into your model … Read more

When to use Spring Security`s antMatcher()?

You need antMatcher for multiple HttpSecurity, see Spring Security Reference: 5.7 Multiple HttpSecurity We can configure multiple HttpSecurity instances just as we can have multiple <http> blocks. The key is to extend the WebSecurityConfigurationAdapter multiple times. For example, the following is an example of having a different configuration for URL’s that start with /api/. @EnableWebSecurity … Read more