- This topic has 1 reply, 2 voices, and was last updated 13 years, 1 month ago by jayperkins.
-
AuthorPosts
-
Andy GormanParticipantI can use some pointers from the Spring experts.
I have been experimenting with using the MESE scaffolding tools for a new project. I am new to Spring and this has been helping me to generate working examples to learn from. So far so good. Now I noticed some odd behavior when I generate a code with REST and JSON support enabled AND there are multiple tables specified (maybe a EMPLOYEE and MANAGER table). In this case some of the RESTful services return JSON and others return XML. For example, this returns JSON:
http://localhost:8080/testApp1/Employee/123
where as this returns XML:
http://localhost:8080/testApp1/Employee/123/manger
In the code that gets generated by the scaffolding tools, how is the view specified? I looked in the web-context.xml that gets generated and that has some viewResolvers, but I don’t understand how it fits together. I assume the some entities are defaulting to XML output and others have JSON explicitly specified. Ultimately my services will have an output=outputType parameter on the URL so the user can specify xml, json, or csv. How should I approach this, using the scaffolded code as a basis.
Thanks for any advice!
Andy
jayperkinsMemberHi Andy,
When spring detects the @ResponseBody annotation, it tries to find a HttpMessageConverter that can write the response. This is typically determined by the http “Accept” header. For example, if you want the result of the rest method invocation to be written to the response as json, you would add “application/json” as an accept header.
If you are making the request directly from the browser without explicitly setting the accept header, the result will be determined by the accept headers sent by the browser which may give you different results depending on the type of object that is being returned.
Let me know if you have any questions.
Jay
-
AuthorPosts