How does responsebody work
Good thing about it is that you can control anything that goes into it. You can specify status code, headers, and body. Next post goes into details of it with a fully working example. Next post goes into details with a full working example.
Spring provides out of box following Http message converters which implements HttpMessageConverter interface [Credit : Spring Reference ]. Form data is read from and written into a MultiValueMap. This can be overridden by setting the supportedMediaTypes property, and overriding getContentType byte[]. This converter requires a Marshaller and Unmarshaller before it can be used.
But either way, somewhere along the request-response cycle, there will be a view produced. But when we use ResponseBody , we are saying that we do not want a view produced. We just want to send the return object as the body, in whatever format we specify.
We wouldn't want it to be a serialized Java object though possible. So yes, it needs to be converted to some other common type this type is normally dealt with through content negotiation - see link below.
Honestly, I don't work much with Spring, though I dabble with it here and there. Normally, I use. Don't quote me, but if you are getting JSON, and you haven't specified the produces , then maybe it is the default. JSON is not the only format. For instance, the above could easily be sent in XML, but you would need to have the produces to MediaType.
I would take some time to learn about Content Negotiation. It's a very important part of REST. It'll help you learn about the different response formats and how to map them to your methods. Try looking at the initial request as see what Accept is set to. Ask Question. Asked 6 years, 8 months ago.
Active 8 months ago. Viewed k times. This method calls a DAO object that returns a List. Improve this question. AndreaNobili AndreaNobili 36k 90 90 gold badges silver badges bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Koray Tugay I want the default always convert to Json. Am I able to do that? JB Nizet can you please explain why http response cannot contain java objects. I am a newbee to java. NavedAli read the http specification, the http response content type defines the legal content that a http response can contain.
Can you point me to source on github, where this decision is done or class name? The first basic thing to understand is the difference in architectures. Normally, I use RequestMapping Sunil Garg Paul Samsotha Paul Samsotha k 33 33 gold badges silver badges bronze badges. I discovered, that I need to omit somehow the view resolver layer. It's not so simple because AbstractController class provides a method that must return view name. I asked a question about that: stackoverflow.
If there is a choice it picks one - in this example, it happens to be JSON. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
0コメント