Spring MVC Controllers Return Type

后端 未结 6 1925
别跟我提以往
别跟我提以往 2020-12-23 11:39

I\'ve seen examples where a controller returns a String (which indicates the view)

@RequestMapping(value=\"/owners/{ownerId}\", method=RequestMethod.GET)
pub         


        
6条回答
  •  暖寄归人
    2020-12-23 12:35

    Returning ModelAndView or a string does the same job. It resolves into a view (a jsp page) and giving a model object to be rendered in the view. If you return a string, spring internally resolves to a view using viewResolver, create a ModelAndView Object and return it. The returning of ModelAndView object is from the older version. You can do either of this based on your convenient.

    Have a look at this question

    Also bunch of answers are pointing out to @RequestBody. Its not exactly related to your question. But keep in mind that it will convert your object based on content-type using available converters and the resuly will be part of the document body.

提交回复
热议问题