Câu hỏi phỏng vấn Spring
Câu hỏi

What are some of the valid retur...

Câu trả lời

In Spring MVC, a controller method can return various types of values, each serving different purposes depending on the specific needs of the application. Here are some of the valid return types that a Spring MVC controller method can use:

  1. String: When a controller method returns a String, it typically represents the name of the view to be rendered. The view name is then resolved by the view resolver, and the corresponding view (like a JSP or a Thymeleaf template) is rendered to the user[1].

  2. ModelAndView: This is a container object that includes both the model data and the view name. It allows a controller to return both the data and the view in one return value. This is useful when you need to send data to the view along with the view name[1][2].

  3. HttpEntity or ResponseEntity: These are used to return full HTTP responses, including the status code, headers, and body. ResponseEntity is a subclass of HttpEntity with additional methods to manipulate HTTP headers and status codes, making it useful for RESTful web services[1][2].

  4. HttpHeaders: This return type allows the method to return only HTTP headers, without a body[1].

  5. void: If a controller method returns void, it means that the method handles the response itself (through response.getWriter() or similar), and Spring MVC does not need to do anything further with the response[1].

  6. @ResponseBody: When a method is annotated with @ResponseBody, the return type can be any object. Spring will automatically serialize this object to JSON or...

expert

expert

Gợi ý câu hỏi phỏng vấn

senior

What are the advantages of Spring MVC over Struts MVC ?

middle

Is the DispatcherServlet instantiated via an application context?

senior

What is the difference between concern and cross-cutting concern in Spring AOP?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào