Switch between JSON (Jackson processor) and XML (XStream) or can i use it simultaneously?
I'm trying to build an Web-Server which can serialize an Objec开发者_开发技巧t into JSON and XML. Since I've integrated Jackson (using an example-Project) I'm able to access an JSON-Serialized Object via my REST Interface, but i'd like to get XML too.
this...
@Controller
@RequestMapping("/main/ajax")
public class AjaxController {
@RequestMapping(value = "/blah", method = RequestMethod.GET, headers = "Accept=application/xml")
public @ResponseBody List<String> blah(@RequestParam(value="input") String input){
List<String> stringList = new LinkedList<String>();
stringList.add("i");
stringList.add("am");
stringList.add("an");
stringList.add("json object");
stringList.add(input);
return stringList;
}
}
whit this query:
http://localhost:8080/spring-json/krams/main/ajax/blah?input=foobar
produces that output:
["i","am","an","json object","foobar"]
Any hints?
UPDATE #1: I implemented the ContentNegotiatingViewResolver...
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<!-- XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
</constructor-arg>
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
<!-- If no extension matched, use JSP view -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
How do i now select either XML or JSON? localhost:8080/spring-json/sometestclass/status ? localhost:8080/spring-json/sometestclass/status.xml ? localhost:8080/spring-json/sometestclass/status.json ?
None of the examples above are working, but i can force the response-format with the Accept-header "application/xml" or "application/json" ... if i do the following...
@RequestMapping(value = "/status", method = RequestMethod.GET, headers = "Accept=application/xml, application/json")
public @ResponseBody Web_ServerStatus isServerAlive() {
Web_ServerStatus l_ReturnObj = new Web_ServerStatus();
...i only get XML back.
What's the problem i am running into?
Thanks in advance!
Yes, take a look at ContentNegotiatingViewResolver (since you included spring as a tag).
Here's a link: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-multiple-representations.
But, REST does not imply XML. Depending on the client's Accept header and the capabilities of you service, you can render the resource in different ways.
Solution:
@RequestMapping(value = "/status", method = RequestMethod.GET)
public Web_ServerStatus isServerAlive() {
...
}
WITHOUT "@ResponseBody"
精彩评论