【问题标题】:Unable to get List into ajax response from Spring MVC无法从 Spring MVC 获取列表到 ajax 响应
【发布时间】:2016-05-27 07:18:14
【问题描述】:

无法从 Spring MVC 获取列表到 ajax 响应,得到 406(不可接受)但能够获取字符串,这是我的代码

@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;
}

阿贾克斯

    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 等,但都得到了相同的结果,任何帮助都将不胜感激。

【问题讨论】:

  • @MadPhysicist 406 (Not Acceptable) on browser console...!
  • 你能发布你的 spring mvc 配置吗?
  • @codependent 请检查我更新的问题。
  • 这只是配置的开始...
  • @codependent 请检查更新的 xml...!

标签: javascript jquery ajax spring spring-mvc


【解决方案1】:

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

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

【讨论】:

  • id="jacksonConverter" 呢,它在哪里使用?
  • Java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException,
  • 我建议在 AbstractMessageConverterMethodProcessor -> protected &lt;T&gt; void writeWithMessageConverters(T returnValue, MethodParameter returnType,ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage) 中调试。检查您是否有 Jackson 消息转换器、媒体类型等。
猜你喜欢
  • 2018-04-03
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 2014-10-29
  • 2020-02-17
  • 2017-07-27
  • 2021-02-07
相关资源
最近更新 更多