dimanche 15 mars 2015

spring 4 ajax response in 406 not acceptable

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:



  1. jackson jars

  2. in RequestMapping annotation : producers as JSON

  3. in RequestMapping annotation : Header as JSON

  4. combination of 2 and 3

  5. $.post instead of $.ajax (i know it doesn't make difference)

  6. My bean object(ValidationResponse) has proper setters and getters

  7. URL doesn't have .htm


jQuery Call in JSP



$.ajax({
url: "register",
data: $('#regForm').serialize(),
type: "POST",
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://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:context="http://ift.tt/GArMu7"
xmlns:tx="http://ift.tt/OGfeU2"
xmlns:mvc="http://ift.tt/1bHqwjR"
xsi:schemaLocation="
http://ift.tt/GArMu6
http://ift.tt/QEDs1e
http://ift.tt/GArMu7
http://ift.tt/QEDs1k
http://ift.tt/1bHqwjR
http://ift.tt/1bVJL9q
http://ift.tt/OGfeU2
http://ift.tt/UJ63uf">

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


Let me know if you need any other detail.


Aucun commentaire:

Enregistrer un commentaire