Http Post with request content type form not working in Spring MVC 3

后端 未结 7 1590
旧时难觅i
旧时难觅i 2020-12-02 11:13

code snippet:

@RequestMapping(method = RequestMethod.POST)//,  headers = \"content-type=application/x-www-form-urlencoded\")
public ModelAndView create(@Req         


        
7条回答
  •  隐瞒了意图╮
    2020-12-02 11:44

    Unfortunately FormHttpMessageConverter (which is used for @RequestBody-annotated parameters when content type is application/x-www-form-urlencoded) cannot bind target classes (as @ModelAttribute can).

    Therefore you need @ModelAttribute instead of @RequestBody. If you don't need to pass different content types to that method you can simply replace the annotation:

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView create(@ModelAttribute UserAccountBean account) { ... }
    

    Otherwise I guess you can create a separate method form processing form data with the appropriate headers attribute:

    @RequestMapping(method = RequestMethod.POST, 
        headers = "content-type=application/x-www-form-urlencoded") 
    public ModelAndView createFromForm(@ModelAttribute UserAccountBean account) { ... }
    

    EDIT: Another possible option is to implement your own HttpMessageConverter by combining FormHttpMessageConverter (to convert input message to the map of parameters) and WebDataBinder (to convert map of parameters to the target object).

提交回复
热议问题