Reputation: 295
Application is developed using Spring MVC 4, Hibernate, and jQuery
Below jquery AJAX call is not giving proper response..it is saying 406 error (not acceptable)
I know it is very old and common problem. I have tried:
jQuery Call in JSP
$.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());
}
});
My Controller
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>
Response header
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
Request 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
Let me know if you need any other detail.
Upvotes: 2
Views: 3055
Reputation: 1114
Add the following dependencies
<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>
Upvotes: 3