Reputation: 59
1) Using Controller at SingleFileUploadController, gives correct result in jsp and when used RestController instead of Controller in SingleFileUploadController, it is not directing to jsp. Why?
2) Is it possible to use both at same time?
reference:
http://memorynotfound.com/spring-mvc-file-upload-example-validator/
Thanks
Harshal
Upvotes: 2
Views: 4260
Reputation: 35
To answer the question in regards to @Controller and @RestController being together.
First controller:
@RestController //specify that this class is a restful controller
@RequestMapping("/api")
public class RestHomeController {
Second Controller
@Controller //specify that this class is a controller
@RequestMapping("/")
public class HomeController {
Upvotes: 2
Reputation: 6574
@Controller tells the api to return ModelAndView Object which contains the name of your view hence the jsp file to view, while @RestController returns serialized response.
No you cannot have them both, controller is either annotated with one of them but as @JB Nizet mentioned you can use @Controller and @ResponseBody to achieve the functionality for @RestController for specific API , anyway this was the trend used since the support for RestController was not there before spring 4.
Upvotes: 0
Reputation: 691735
Because RestController is for controllers who don't forward to views. Their return value is sent as the response body.
Yes, it's possible to have Controllers and RestControllers in the same webapp. If you want some methods of your controller to return views, and some others to return response bodies (i.e. act as in a RestController), then use @Controller
, and annotate your "REST" methods with @ResponseBody
.
Upvotes: 7