Spring MVC Parameters and X-HTTP Method-Override

I am using Spring MVC and I have a function to update a user profile:

@RequestMapping(value = "/{userName}" + EndPoints.USER_PROFILE, method = RequestMethod.PUT) public @ResponseBody ResponseEntity<?> updateUserProfile( @PathVariable String userName, @RequestBody UserProfileDto userProfileDto) { // Process update user profile } 

I started using JMeter, and for some reason they had a problem sending a PUT request with the body (either to the request body, or by hacking the request parameter).

I know that in Jersey you can add a filter to handle the request parameter of the HTTP override method so that you can send a POST request and override it with the header parameter.

Is there a way to do this in Spring MVC?

Thanks!

+4
source share
2 answers

Spring MVC has a HiddenHttpMethodFilter that allows you to include a request parameter ( _method ) to override the http method, you just need to add the filter to the filter chain in web.xml.

I donโ€™t know about the ready-made solution to use the X-HTTP-Method-Override header, but you can create a filter similar to HiddenHttpMethodFilter yourself, which uses the header to change the value, not the request parameter.

+9
source

You can use this class as a filter:

 public class HttpMethodOverrideHeaderFilter extends OncePerRequestFilter { private static final String X_HTTP_METHOD_OVERRIDE_HEADER = "X-HTTP-Method-Override"; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String headerValue = request.getHeader(X_HTTP_METHOD_OVERRIDE_HEADER); if (RequestMethod.POST.name().equals(request.getMethod()) && StringUtils.hasLength(headerValue)) { String method = headerValue.toUpperCase(Locale.ENGLISH); HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method); filterChain.doFilter(wrapper, response); } else { filterChain.doFilter(request, response); } } private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper { private final String method; public HttpMethodRequestWrapper(HttpServletRequest request, String method) { super(request); this.method = method; } @Override public String getMethod() { return this.method; } } } 

Source: http://blogs.isostech.com/web-application-development/put-delete-requests-yui3-spring-mvc/

+2
source

Source: https://habr.com/ru/post/1468744/


All Articles