2013-03-09 78 views
0

我正在尝试使用ResponseBody返回json和xml响应,并且它对xml确实工作正常,但不返回json。我对XML请求URI是 '../home.xml' 和JSON是从控制器的方法 '../home.json':json响应不会使用spring返回3.1

@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,显示消息的完全限定名称? 我正在使用Spring 3.1。

编辑: 有趣的是,它确实在请求类型设置正确时创建了json n xml。但仍然存在第二个问题。

+0

什么是使用的请求标头? – 2013-03-09 10:23:23

+0

你需要发送请求头'Accept = application/json' – 2013-03-09 10:23:56

+0

请求头是他们的默认值,我还没有改变它的头。此外,我想只需调用url而不更改标题中的任何内容。可能吗?感谢您的快速回复。 – 2013-03-09 10:27:37

回答

1

我不知道你试图做些什么,所以我不知道我能不能帮忙,但我会给它一个镜头。

首先,您可以尝试添加接受标头。下面是一个例子。

@RequestMapping(value = "/home/yourpath", method = RequestMethod.GET, 
headers = "Accept=application/xml, application/json") 

通过添加接受头JSON和XML请求将与卷曲客户机调用您的方法和解析数据例如当被接受。也许你应该考虑明确地设置一个请求方法。

当您向客户端发送请求时,请确保根据您发送的内容将“Content-Type:application/json”或“Content-Type:application/xml”添加到请求头中。否则,你会得到不支持的内容类型错误,因为你可能发送了错误的内容类型。

在Spring中有两种实际显示正确内容类型的方法。

1:通过解析/接受请求头中的正确内容类型(通过配置客户端中的接受头文件)。

2:通过配置文件扩展名(有些特定的默认文件扩展名应该可以工作,包括xml和json)。尝试将.xml或.json添加到您的uri的末尾。

看一看这里:http://static.springsource.org/spring/docs/3.1.0.RELEASE/reference/htmlsingle/#mvc-multiple-representations

请看下面的例子: http://www.mkyong.com/spring-mvc/spring-3-mvc-contentnegotiatingviewresolver-example/

我不能帮你的第二个问题。

我希望这会有所帮助。祝你好运。

亲切的问候, 克里斯

+0

感谢您的答复。是的,我需要添加内容类型以确保解决第一个问题,而不会在我显示的代码的其余部分进行更改。我试图修正它,但没有定义Content-Type。但看起来像是必要的定义。此外,它也确定了第二个问题。 – 2013-03-10 15:13:11