I have discovered an issue with Spring data binding where a real PUT request doesn't bind correctly to a ModelAttribute whereas a hidden one does (ie, POST with parameter _method=put) when using a HiddenHttpMethodFilter in my servlet.
My controller class is:
When I send an x-www-form-urlencoded POST request with _method=put along with any parameters to modify the group, the values are correctly mapped to the ModelAttribute Group. However, when I send a x-www-form-urlencoded PUT request with the same parameters to modify the group, the group is unchanged showing none of the parameters have been bound to the ModelAttribute object fetched by setup.Code:@Transactional(readOnly = false) @RequestMapping(value = "/group/{id}",method = RequestMethod.PUT) public @ResponseBody Group update(@ModelAttribute Group group) { return directoryService.persist(group); } @ModelAttribute public Group setup(@PathVariable String id) { return StringUtils.isEmpty(id) ? new Group() : directoryService.get(Group.class, id); }
It is my understanding that in HTTP, a PUT is allowed to wrap entities like a POST and should therefore bind data the same as a POST within spring.
I considered that the HiddenHttpMethodFilter might be the issue and so added PUT as a method that should be wrapped:
but the appears to make no difference and instead points to an issue with the way data is bound for POSTs vs PUTs deeper within spring.Code:protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String paramValue = request.getParameter(this.methodParam); if (("POST".equals(request.getMethod()) || "PUT".equals(request.getMethod())) && StringUtils.hasLength(paramValue)) { String method = paramValue.toUpperCase(Locale.ENGLISH); HttpServletRequest wrapper = new HttpMethodRequestWrapper(method, request); filterChain.doFilter(wrapper, response); } ...
Has anyone:
1) experienced this before,
2) has a solution, or
3) has an explanation for why this is happening?
Cheers,
Luke


Reply With Quote

