Working with Frames: Spring3 MVC + dojo1.8
Premise: Configure spring MVC to respond to requests in the form of JSON data
Usage Scenario: Dojo sends Ajax requests to spring MVC
Exception information:
Org.springframework.web.HttpMediaTypeNotSupportedException:Content type ' application/x-www-form-urlencoded; Charset=utf-8 ' not supported at Org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConve Rters (abstractmessageconvertermethodargumentresolver.java:124) at Org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConve Rters (abstractmessageconvertermethodargumentresolver.java:91) at Org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument ( REQUESTRESPONSEBODYMETHODPROCESSOR.JAVA:71) at Org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument ( HANDLERMETHODARGUMENTRESOLVERCOMPOSITE.JAVA:75) at Org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues ( invocablehandlermethod.java:156) at Org.springframework.web.method.support.InvocableHandlerMethod.Invokeforrequest (invocablehandlermethod.java:117) at Org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle ( servletinvocablehandlermethod.java:96) at Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod ( requestmappinghandleradapter.java:617) at Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal ( requestmappinghandleradapter.java:578) at Org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle ( ABSTRACTHANDLERMETHODADAPTER.JAVA:80) at Org.springframework.web.servlet.DispatcherServlet.doDispatch ( dispatcherservlet.java:923) at Org.springframework.web.servlet.DispatcherServlet.doService ( dispatcherservlet.java:852) at Org.springframework.web.servlet.FrameworkServlet.processRequest (
frameworkservlet.java:882) at Org.springframework.web.servlet.FrameworkServlet.doPost (frameworkservlet.java:790) At JAVAX.SERVLET.HTTP.HTTPSERVLEt.service (httpservlet.java:641) at Javax.servlet.http.HttpServlet.service (httpservlet.java:723) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:304) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:210) at Org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal (hiddenhttpmethodfilter.java:77) at Org.springframework.web.filter.OncePerRequestFilter.doFilter (onceperrequestfilter.java:76) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:243) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:210) at Org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal (characterencodingfilter.java:89) at Org.springframework.web.filter.OncePerRequestFilter.doFilter (onceperrequestfilter.java:76) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:243) AT Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:210) at Org.apache.catalina.core.StandardWrapperValve.invoke (standardwrappervalve.java:240) at Org.apache.catalina.core.StandardContextValve.invoke (standardcontextvalve.java:164) at Org.apache.catalina.authenticator.AuthenticatorBase.invoke (authenticatorbase.java:463) at Org.apache.catalina.core.StandardHostValve.invoke (standardhostvalve.java:168) at Org.apache.catalina.valves.ErrorReportValve.invoke (errorreportvalve.java:100) at Org.apache.catalina.valves.AccessLogValve.invoke (accesslogvalve.java:564) at Org.apache.catalina.core.StandardEngineValve.invoke (standardenginevalve.java:120) at Org.apache.catalina.connector.CoyoteAdapter.service (coyoteadapter.java:399) at Org.apache.coyote.http11.Http11Processor.process (http11processor.java:317) at Org.apache.coyote.http11.http11protocol$http11connectionhandler.process (http11protocol.java:204) at Org.apache.tomcat.util.net.jioendpoint$socketprocEssor.run (jioendpoint.java:311) at Java.util.concurrent.threadpoolexecutor$worker.runtask (
threadpoolexecutor.java:886) at Java.util.concurrent.threadpoolexecutor$worker.run (ThreadPoolExecutor.java:909) At Java.lang.Thread.run (thread.java:619)
Cause: dojo1.8 The AJAX request type sent by default is ' Application/x-www-form-urlencoded;charset=utf-8 ', but spring MVC uses ' application/ JSON ', so it needs to be changed to ' Content-type ' in the AJAX request of Dojo: ' Application/json;charset=utf-8 ', the following code headers block:
Require (["DOJO/REQUEST/XHR", "Dojo/json"], function (XHR) {
var userInfo = {
"userName": User.value,
" Password ": Passwd.value
};
Xhr.post ("/springmvcrestful/login", {
data:JSON.stringify (userInfo),
//query:json.stringify (userInfo),/ /plus this will show the parameters in the URL
sync:true,
handleas: "JSON",
timeout:200000,
headers: {
' content-type ': ' Application/json;charset=utf-8 '
}
}). Then (function (response) {
alert (response);
}, function ( ERR) {
alert ("ERR:" + err);
});
Problem solving.
Reference: Stackover