SPRINGMVC 406 error in resolving AJAX requests

Source: Internet
Author: User

Straight into the topic-

Page form Asynchronous commit: $.post (' <%=request.getcontextpath ()%>/web/register.html ', $ (form). Serialize (), function (data) {... });

The controller of the Java backend SPRINGMVC accepts the request:

@RequestMapping ("register")
@ResponseBody
Public map<string, string> Register (httpservletrequest request, httpservletresponse response, user user) {
map<string, string> resmap = loginservice.insertuser (user);
return resmap;
}

Error description, the SPRINGMVC controller receives the AJAX request sent by the page, and the Java background processing times is wrong:


[DEBUG] [2016-02-14 20:08:09] Org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException ( 133) | Resolving exception from handler [[email protected]]:Org.springframework.web.HttpMediaTypeNotAcceptableException:Could not find acceptable representation
[DEBUG] [2016-02-14 20:08:09] Org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException ( 133) | Resolving exception from handler [[email protected]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
[DEBUG] [2016-02-14 20:08:09] Org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException ( 133) | Resolving exception from handler [[email protected]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
[DEBUG] [2016-02-14 20:08:09] Org.springframework.web.servlet.DispatcherServlet.processDispatchResult (1034) |Null Modelandview returned to Dispatcherservlet with Name ' rest ': Assuming Handleradapter completed request handling
[DEBUG] [2016-02-14 20:08:09] org.springframework.web.servlet.FrameworkServlet.processRequest (999) | Successfully completed request
[DEBUG] [2016-02-14 20:08:09] Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (251) | Returning cached instance of singleton Bean ' sqlsessionfactory '

At this point the corresponding page error is:

POST http://127.0.0.1:8080/maven-web-demo/web/register.html 406 (not acceptable)

Cause: Missing JSON configuration for SPRING-MVC

Workaround:

1. Add Spring-mvc JSON dependency jar package: Jackson-core and Jackson-databind

2. Add in Spring-mvc.xml:

<bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name= "Messageconverters" >
<list>
<ref bean= "Mappingjacksonhttpmessageconverter"/>
</list>
</property>
</bean>
<bean id= "Mappingjacksonhttpmessageconverter" class= " Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter ">
<property name= "Supportedmediatypes" >
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

OK, problem solved!

SPRINGMVC 406 error in resolving AJAX requests

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.