2012-02-22 38 views
0

我已經包括傑克遜映射在我的POM文件獲得spring mvc 3.1輸出json和xml響應所需的最小數量是多少?

 <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>1.9.2</version> 
     </dependency> 

也爲XML我包括我的POM文件

 <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-oxm</artifactId> 
      <version>3.1.0.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>com.thoughtworks.xstream</groupId> 
      <artifactId>xstream</artifactId> 
      <version>1.4.2</version> 
     </dependency> 

兩個春天OXM和XStream和後續是我的Spring MVC的配置文件:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:oxm="http://www.springframework.org/schema/oxm" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
      http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"> 


    <mvc:resources mapping="/static/js/**" location="/static/js/"/> 
    <mvc:resources mapping="/favicon.ico" location="/favicon.ico"/> 

    <mvc:annotation-driven /> 
    <context:component-scan base-package="com.xyz.web.controllers"/> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/views/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 
</beans> 

每當我試圖讓JSON或XML響應,我得到這個

DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public com.xyz.web.controllers.User com.xyz.web.controllers.UserController.get(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 

任何幫助,非常感謝!

+1

此信息對您有幫助 http://blog.springsource.org/2010/01/25/ajax-simplifications-in-spring-3-0/ – 2012-02-22 02:42:57

+0

好吧,根據那篇文章「json serialization」已啓用當您使用帶有存在於您的類路徑中的Jackson的mvc:註釋驅動配置元素時自動執行「。我相信我符合這兩個標準。 – Bobo 2012-02-22 14:24:29

回答

0

默認支持做的註解,你對你的控制器方法。

我有以下的,一切都很好:

@RequestMapping("carrierCompanies_json") 
@ResponseBody 
public Map<String, String> getValuesAsJSON() { 
    return shipperService.getInfoForUser(getUser()); 
} 
0

我所做的是:

我只是說傑克遜依賴於我的POM文件,然後@ResponseBody添加:

@RequestMapping(value = "/test", method = RequestMethod.GET) 
@ResponseBody 
public Map<String, String> getTestValue() { 
    Map<String, String> map = new HashMap<String, String>(); 
    map.put("1", "dfd"); 
    map.put("2", "dfd"); 
    map.put("3", "dfd"); 
    map.put("4", "dfd"); 
    return map; 
} 

然後我得到了預期的結果: { 「1」: 「DFD」, 「2」: 「DFD」, 「3」:「dfd」, 「4」:「dfd」 }

順便說一句,我用的是spring mvc 3.1。

之前我使用Spring MVC的3.1,我用3.0,當我做了上面,我也不得不添加以下.xml文件:

<beans:bean id="stringHttpMessageConverter" 
    class="org.springframework.http.converter.StringHttpMessageConverter" /> 

<beans:bean id="jsonHttpMessageConverter" 
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 

<beans:bean id="methodHandlerExceptionResolver" 
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"> 
    <beans:property name="messageConverters"> 
     <beans:list> 
      <beans:ref bean="stringHttpMessageConverter" /> 
      <beans:ref bean="jsonHttpMessageConverter" /> 
     </beans:list> 
    </beans:property> 
</beans:bean> 

我希望這可以幫助你。

相關問題