无法将列表从Spring MVC转换为Ajax响应 [英] Unable to get List into ajax response from Spring MVC

查看:143
本文介绍了无法将列表从Spring MVC转换为Ajax响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法将列表从Spring MVC的ajax响应中获取406(不可接受),但能够获取String,这是我的代码

Unable to get List into ajax response from Spring MVC getting 406 (Not Acceptable) but able to get String, here my code

@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等,但要获得相同的帮助,将不胜感激.

Have try many things Like Map,Json etc but getting the same, any help will be appreciate.

推荐答案

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

Add the following bean to your configuration:

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

这篇关于无法将列表从Spring MVC转换为Ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆