我正在尝试使用 ResponseBody 返回 json 和 xml 响应,它确实适用于 xml,但不返回 json。我对 xml 的请求 uri 是 '../home.xml' 而对于 json 是来自控制器方法的 '../home.json':
@RequestMapping("home.*")
public @ResponseBody Message homeOther(HttpServletRequest request, HttpServletResponse response, ModelMap mv){
Message msg = new Message();
msg.setDetail("I am here at home");
msg.setUploadDate(new Date());
mv.addAttribute("message", msg);
return msg;
}
这是调度程序 servlet:
<context:component-scan base-package="com.ym"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="alwaysUseFullPath" value="true"/>
</bean>
<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/views"/>
</bean>
<!-- Simple ViewResolver for Velocity, appending ".vm" to logical view names -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="order" value="2" />
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".vm"/>
<property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/>
<!-- if you want to use the Spring Velocity macros, set this property to true -->
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="contentType" value="text/html; charset=UTF-8" />
</bean>
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller" />
<bean id="xmlView" class="org.springframework.web.servlet.view.xml.MarshallingView"
>
<constructor-arg ref="xstreamMarshaller" />
</bean>
<bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonHttpMessageConverter" />
<ref bean="marshallingHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="xstreamMarshaller"/>
<property name="unmarshaller" ref="xstreamMarshaller"/>
</bean>
<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">
</bean>
<ref bean="xmlView" />
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
这是 XML 响应:
<com.ym.mongodb.model.Message>
<messageId>0</messageId>
<detail>I am here at home</detail>
<uploadDate>2013-03-09 09:56:46.606 UTC</uploadDate>
</com.ym.mongodb.model.Message>
我的问题是:1.配置有什么问题?为什么它不返回json响应。?2. 为什么它返回的xml,显示Message的完全限定名?我正在使用 Spring 3.1。
编辑:有趣的是,当请求类型设置正确时,它确实会正确创建 json n xml。但是第二个问题仍然存在。