Java 406中的Spring4Ajax响应不可接受

Java 406中的Spring4Ajax响应不可接受,java,jquery,ajax,json,spring,Java,Jquery,Ajax,Json,Spring,应用程序是使用SpringMVC4、Hibernate和jQuery开发的 下面jquery AJAX调用没有给出正确的响应。它说的是406错误(不可接受) 我知道这是一个很老很常见的问题。我试过: 杰克逊瓶 在RequestMapping注释中:作为JSON的生产者 在RequestMapping注释中:标题为JSON 2和3的组合 $.post而不是$.ajax(我知道这没什么区别) 我的bean对象(ValidationResponse)具有适当的setter和getter URL没有.h

应用程序是使用SpringMVC4、Hibernate和jQuery开发的

下面jquery AJAX调用没有给出正确的响应。它说的是406错误(不可接受)

我知道这是一个很老很常见的问题。我试过:

  • 杰克逊瓶
  • 在RequestMapping注释中:作为JSON的生产者
  • 在RequestMapping注释中:标题为JSON
  • 2和3的组合
  • $.post而不是$.ajax(我知道这没什么区别)
  • 我的bean对象(ValidationResponse)具有适当的setter和getter
  • URL没有.htm文件
  • 添加accept JSON头,如 接受:{ json:'application/json', xml:“应用程序/xml” },
  • JSP中的jQuery调用

     $.ajax({
                    url: "register",    
                    data: $('#regForm').serialize(), 
                    type: "POST",
                     accept: {
                      json: 'application/json',
                      xml: 'application/xml'
                },
                    success: function(result) {  
                    // success message
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown){
                        alert(XMLHttpRequest.toString());
    
                    }
                });
    
    我的控制器

    @RequestMapping(value="/register", 
                method   = RequestMethod.POST)
        public @ResponseBody 
        ValidationResponse register(@Valid @ModelAttribute("user") User user, BindingResult result, ModelMap map) {
            ValidationResponse res = new ValidationResponse();
    
            // Saving into DB logic
            return res; // till here I can see all the values correctly while debugging
        }
    
    登录控制器

    @RequestMapping(value="/register", 
                method   = RequestMethod.POST)
        public @ResponseBody 
        ValidationResponse register(@Valid @ModelAttribute("user") User user, BindingResult result, ModelMap map) {
            ValidationResponse res = new ValidationResponse();
    
            // Saving into DB logic
            return res; // till here I can see all the values correctly while debugging
        }
    
    验证响应bean

    public class ValidationResponse {
    
        private String status;
        private ErrorMessage[] errorMessageList;
    
    //getter and setter
    }
    
    错误消息bean

    public class ErrorMessage {
    
        private String fieldName;
        private String message;
    //getter and setters
    }
    
    app-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
                            http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    
        <mvc:annotation-driven />
        <mvc:resources mapping="/assets/**" location="/assets/"  />
    
        <context:property-placeholder location="classpath:database.properties" />
        <context:component-scan base-package="com.app" />
    
        <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
    
        <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="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
                </props>
            </property>
            <property name="packagesToScan" value="com.app"></property>
        </bean>
    
        <!-- bind your messages.properties -->
        <bean class="org.springframework.context.support.ResourceBundleMessageSource"
            id="messageSource">
            <property name="basename" value="messages" />
        </bean>
    
    </beans>
    
    请求头

    HTTP/1.1 406 Not Acceptable
    Server: Apache-Coyote/1.1
    Content-Type: text/html;charset=utf-8
    Content-Length: 1074
    Date: Mon, 16 Mar 2015 03:56:02 GMT
    
    POST /app/register HTTP/1.1
    Host: localhost:8080
    Connection: keep-alive
    Content-Length: 104
    Accept: text/json; text/html; charset=utf-8
    Origin: http://localhost:8080
    X-Requested-With: XMLHttpRequest
    User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36
    Content-Type: text/json; text/html; charset=UTF-8
    Referer: http://localhost:8080/app/home
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.8
    

    如果您需要任何其他详细信息,请告诉我。

    添加以下依赖项

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.5.1</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.5.1</version> 
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.5.1</version>
    </dependency>
    
    
    com.fasterxml.jackson.core
    杰克逊核心
    2.5.1
    com.fasterxml.jackson.core
    杰克逊数据绑定
    2.5.1 
    com.fasterxml.jackson.core
    杰克逊注释
    2.5.1
    
    服务器似乎无法生成客户端接受的MIME类型。在发送ajax请求时尝试设置accepts属性。也尝试了此操作。添加到上面的列表中“accepts”也不起作用。你错过了“s”之后,它也不起作用。。我也使用了这个…..标题:{接受:“text/json;text/html;charset=utf-8”,“内容类型”:“text/json;text/html;charset=utf-8”,}