Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning view from Spring MVC @RestController

As @RestController is composition of @Controller and @ResponseBody, I believe if I want my controller to work as both MVC and REST controller just annotating with @RestController should be fine. Is that correct?

As @RestController is composition of @Controller and @ResponseBody, I think it internally means that it's good for

  1. Receiving the http request (because of @Controller)
  2. Sending the response in JSON format (because of @ResponseBody) though it can be changed if required
like image 506
M Sach Avatar asked Oct 11 '15 07:10

M Sach


People also ask

Can we return view from RestController?

In @RestController, we can not return a view. @Controller annotation indicates that the class is a “controller” like a web controller.

Can we use RestController in Spring MVC?

Spring RestController annotation is used to create RESTful web services using Spring MVC. Spring RestController takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON or XML response.

What is the return type of RestController?

1.2. @RestController. As name suggest, it shall be used in case of REST style controllers i.e. handler methods shall return the JSON/XML response directly to client rather using view resolvers. It is a convenience annotation that is itself annotated with @Controller and @ResponseBody .

Can we replace @RestController with @controller?

So if you are using latest spring version you can directly replace @Controller with @RestController without any issue.


2 Answers

@RestController is not meant to be used to return views to be resolved. It is supposed to return data which will be written to the body of the response, hence the inclusion of @ResponseBody. You can not selectively disable the @ResponseBody on individual handler methods when @ResponseBody is already annotation on class level.

You can work around it by returning ModelAndView, which will work even in @RestController, but you really shouldn't:

@RequestMapping
public ModelAndView renderFooList() {
    ModelAndView mav = new ModelAndView("foo/list");
    mav.addObject("foos", fooService.getFoos());
    return mav;
}

It would be better to create separate controllers for normal handlers returning views and REST controllers for the RESTful stuff. Or to annotate the class with plain @Controller and put @ResponseBody on the methods where you actually need it.

like image 195
Bohuslav Burghardt Avatar answered Oct 22 '22 08:10

Bohuslav Burghardt


@RestController annotation, which marks this class as a controller where every method returns a domain object/pojo instead of a view. It means that we are no more using view-resolvers, we are no more directly sending the html in response but we are sending domain object converted into format understood by the consumers.

like image 26
NightsWatch Avatar answered Oct 22 '22 07:10

NightsWatch