2013-03-09 79 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