2012-10-19 77 views
0

我有問題讓Jackson在我的Spring應用程序中工作。傑克遜在Spring MVC 3.1.2

我使用

  • Spring MVC的3.1.2
  • 傑克遜1.9.1映射翔升

我已經添加了傑克遜庫/ WEB-INF/lib目錄/文件夾並添加到我的spring配置文件中。

春天-config.xml中

<!-- language: xml--> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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"> 

<mvc:annotation-driven /> 

<context:component-scan base-package="com.mason.server.controller" /> 

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

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="WEB-INF/messages/messages" /> 
</bean> 

功能控制器

<!-- language: java --> 
@RequestMapping(value = "/get_json", method = RequestMethod.GET, headers = "Accept=*/*") 
public @ResponseBody List<String> getTechList() { 
    List<String> countryList = new ArrayList<String>(); 

    countryList.add("test"); 

    return countryList; 
} 

,當我去到localhost:8888/get_json我得到一個錯誤406

我已經嘗試了互聯網上的解決方案,但他們似乎都沒有工作。任何幫助,將不勝感激! PS:我將Spring MVC與Google App Engine和Spring Security結合使用。

+0

我有我的工作,我很快會上傳我的解決辦法 – areont

回答

2

2天后,我已經得到它的工作。我忘了將jackson-core-asl-1.9.10添加到我的庫中,當我使用jQuery發出請求時,一切正常。它不工作去在瀏覽器中的鏈接,但我想現在很好。

MVC-config.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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"> 

<context:component-scan base-package="com.mason.server.controller" /> 

<mvc:annotation-driven /> 

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
    <property name="mediaTypes"> 
    <map> 
     <entry key="html" value="text/html"/> 
     <entry key="json" value="application/json"/> 
    </map> 
    </property> 
    <property name="viewResolvers"> 
    <list> 
     <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
     <property name="prefix" value="/WEB-CONTENT/"/> 
     <property name="suffix" value=".jsp"/> 
     </bean> 
    </list> 
    </property> 
    <property name="defaultViews"> 
    <list> 
     <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"> 
     <property name="prefixJson" value="true"/> 
     </bean> 
    </list> 
    </property> 
</bean> 

test.jsp的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
<html xmlns=" http://www.w3.org/1999/xhtml "> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Request json test</title> 
<script src="http://code.jquery.com/jquery-1.5.js"></script> 
<script> 

$(document).ready(function(){ 
    //attach a jQuery live event to the button 
    $('#getdata-button').live('click', function(){ 
     $.getJSON('/get_json', function(data) { 
      //alert(data); //uncomment this for debug 
      //alert (data.item1+" "+data.item2+" "+data.item3); //further debug 
      $('#showdata').html("<p>"+data+"</p>"); 
     }); 
    }); 
}); 

</script> 

</head> 
<body> 
<a href="#" id="getdata-button">Get JSON Data</a> 
<div id="showdata"></div> 
</body> 
</html>