杰克逊在Spring MVC 3.1.2

时间:2012-10-19 15:13:29

标签: spring-mvc jackson

我遇到让Jackson在我的Spring应用程序中工作的问题。

我正在使用

  • Spring MVC 3.1.2
  • Jackson 1.9.1 mapper asl

我已将Jackson库添加到/ 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结合使用。

1 个答案:

答案 0 :(得分: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>