无法从Spring MVC获取List的ajax响应

时间:2016-05-27 07:18:15

标签: javascript jquery ajax spring spring-mvc

无法从Spring MVC获取List到ajax响应获得406(不可接受)但能够获取String,这里是我的代码

@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = "/getCategory", method = RequestMethod.POST, headers = "Accept=*/*", produces = "application/json")
public @ResponseBody List<Info> initCategory(@ModelAttribute(value = "getCategories") Info info) {
    List<Info> category = null;
    try {
        category = infoService.getCategory(info.getPlantName());
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return category;
}

Ajax

    function selectCategory() {
        var plantId = $('#plantId').val();
        alert(plantId);
        $.ajax({
                    url : '/veQNL/addPro/getCategory',
                    type : 'POST',
                    data : $("#addProject").serialize(),
                    success : function(result) {
                        alert(result);
                    }
        });
}

和调度员

<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-4.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.0.xsd
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!--registration and scanning all controller ,service ,repository annotation -->
<context:component-scan base-package="com.ve.qnl" />
<!-- jdbc resource -->

<context:property-placeholder location="classpath:resources/database.properties" />
<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/"
    cache-period="31556926" />
<mvc:annotation-driven /></beans>

尝试了许多像Map,Json等的东西,但是得到同样的东西,任何帮助都会很感激。

1 个答案:

答案 0 :(得分:0)

将以下bean添加到您的配置中:

<bean id="jacksonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />