Sobes.tech
Junior — Middle

What categories of data can a handler produce in Spring MVC?

sobes.tech AI

Answer from AI

In Spring MVC, a handler (controller) can return various data types, which are then converted into a response for the client:

  • ModelAndView — contains the data model and the view name, used for generating HTML pages.
  • String — usually the name of the view.
  • Objects (POJO) — automatically serialized into JSON or XML when using annotations like @ResponseBody or @RestController.
  • ResponseEntity<T> — allows control over HTTP status, headers, and response body.
  • void — if the response is formed directly through HttpServletResponse.

Thus, the handler can output both display data (model and view) and ready data in JSON/XML format for APIs.