What are some of the valid return types of a controller method?

Technology CommunityCategory: SpringWhat are some of the valid return types of a controller method?
VietMX Staff asked 3 years ago

There are many return types are available for a controller method in Spring MVC, which is annotated by @RequestMappinginside the controller. Some of the popular ones are:

  1. String
  2. void
  3. View
  4. ModelAndView (Class)
  5. Model (Interface)
  6. Map
  7. HttpEntity or ResponseEntity
  8. HttpHeaders

Every return type has its specific use. For example, if you are using String, then it means that the Controller will just return the View Name and this view name will be resolved by ViewResolver.

If you don’t want to return any view name, mention return type void. If you want to set a view name as well as want to send a object, use ModelAndViewas a return type.