春天4 ajax在406的反应是不可接受的

时间:2015-03-15 17:04:10

标签: java jquery ajax json spring

使用Spring MVC 4,Hibernate和jQuery开发应用程序

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

我知道这是一个非常古老和常见的问题。我试过了:

  1. jackson jars
  2. RequestLapping注释中的
  3. :生产者为JSON
  4. RequestLapping注释中的
  5. :标题为JSON
  6. 2和3的组合
  7. $。发帖而不是$ .ajax(我知道它没有什么区别)
  8. 我的bean对象(ValidationResponse)有适当的setter和getters
  9. 网址没有.htm
  10. 添加接受JSON标头之类的 接受:{                   json:' application / json',                   xml:' application / xml'             },
  11. 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());
    
                    }
                });
    

    我的控制器

    的LoginController

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

    ValidationResponse bean

    public class ValidationResponse {
    
        private String status;
        private ErrorMessage[] errorMessageList;
    
    //getter and setter
    }
    

    Errormessage 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
    

    如果您需要任何其他细节,请与我们联系。

1 个答案:

答案 0 :(得分:3)

添加以下依赖项

<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>