Spring: @ModelAttribute VS @RequestBody

后端 未结 7 487
闹比i
闹比i 2020-12-04 16:19

Please correct me if I am wrong. Both can be used for Data Binding.

The question is when to use @ModelAttribute?

@RequestMapping(val         


        
7条回答
  •  再見小時候
    2020-12-04 17:09

    I find that @RequestBody (also annotating a class as @RestController) is better for AJAX requests where you have complete control over the contents of the request being issued and the contents are sent as either XML or JSON (because of Jackson). This allows the contents to easily create a model object. Conversely, @ModelAttribute seems to be better suited to forms where there is a "command" object backing a form (which may not necessarily be a model object).

提交回复
热议问题