gpt4 book ai didi

java - 406 中的 spring 4 ajax 响应 Not Acceptable

转载 作者:行者123 更新时间:2023-11-30 08:15:12 25 4
gpt4 key购买 nike

应用程序是使用 Spring MVC 4、Hibernate 和 jQuery 开发的

下面的 jquery AJAX 调用没有给出正确的响应..它说 406 错误( Not Acceptable )

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

  1. jackson jar
  2. 在 RequestMapping 注释中:生产者作为 JSON
  3. 在RequestMapping注解中:Header as JSON
  4. 2 和 3 的组合
  5. $.post 而不是 $.ajax(我知道这没有区别)
  6. 我的 bean 对象(ValidationResponse)有适当的 setter 和 getter
  7. URL 没有.htm
  8. 添加接受 JSON header ,例如接受: { json: '应用程序/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());

}
});

我的 Controller

登录 Controller

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

应用程序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

请求 header

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>

关于java - 406 中的 spring 4 ajax 响应 Not Acceptable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com