Can we send an Object as the response of Controller handler method?
Can we send an Object as the response of Controller handler method?
Yes, you can send an object as the response of a controller handler method in Spring MVC. This is typically done using the @ResponseBody annotation on the handler method. The @ResponseBody annotation tells Spring that the return type should be written directly to the HTTP response body, and not placed in a Model, or interpreted as a view name.
When you use @ResponseBody, Spring uses HTTP Message Converters to convert the return value to HTTP response body, based on the content-type in the request HTTP header. Commonly, this involves converting the return object to JSON or XML format.
For example, if you have a controller method that returns an instance of a User class, you can annotate this method with @ResponseBody, and Spring will automatically serialize the User object into JSON format and write it to the response body:
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.