本文介绍了使用jQuery将JSON对象发布到Spring 3控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Spring 3中将JSON对象发布到控制器。

I'm trying to POST a JSON object to a controller in Spring 3.

我收到以下错误:

2010-10-20 17:59:14,391 DEBUG [org.springframework.web.servlet.DispatcherServlet] Could not complete request
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized token 'tilI': was expecting 'null', 'true' or 'false'
 at [Source: org.apache.catalina.connector.CoyoteInputStream@1509a99; line: 1, column: 9]; nested exception is org.codehaus.jackson.JsonParseException: Unrecognized token 'tilI': was expecting 'null', 'true' or 'false'
 at [Source: org.apache.catalina.connector.CoyoteInputStream@1509a99; line: 1, column: 9]
 at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:138)
 at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:643)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:607)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654)
 at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445)
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)
 at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
 at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
 at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
 at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
 at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
 at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
 at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.jackson.JsonParseException: Unrecognized token 'tilI': was expecting 'null', 'true' or 'false'
 at [Source: org.apache.catalina.connector.CoyoteInputStream@1509a99; line: 1, column: 9]
 at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1150)
 at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:324)
 at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidToken(Utf8StreamParser.java:1220)
 at org.codehaus.jackson.impl.Utf8StreamParser._matchToken(Utf8StreamParser.java:1188)
 at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:184)
 at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2017)
 at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1974)
 at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1331)
 at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)
 ... 42 more
2010-10-20 17:59:14,391 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/cfs].[Spring MVC Dispatcher Servlet]] Servlet.service() for servlet Spring MVC Dispatcher Servlet threw exception
org.codehaus.jackson.JsonParseException: Unrecognized token 'tilI': was expecting 'null', 'true' or 'false'
 at [Source: org.apache.catalina.connector.CoyoteInputStream@1509a99; line: 1, column: 9]
 at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1150)
 at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:324)
 at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidToken(Utf8StreamParser.java:1220)
 at org.codehaus.jackson.impl.Utf8StreamParser._matchToken(Utf8StreamParser.java:1188)
 at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:184)
 at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2017)
 at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1974)
 at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1331)
 at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)
 at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:643)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:607)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654)
 at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445)
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)
 at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
 at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
 at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
 at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
 at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
 at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
 at java.lang.Thread.run(Thread.java:662)
2010-10-20 17:59:14,393 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/cfs].[default]] Servlet.service() for servlet default threw exception
org.codehaus.jackson.JsonParseException: Unrecognized token 'tilI': was expecting 'null', 'true' or 'false'
 at [Source: org.apache.catalina.connector.CoyoteInputStream@1509a99; line: 1, column: 9]
 at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1150)
 at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:324)
 at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidToken(Utf8StreamParser.java:1220)
 at org.codehaus.jackson.impl.Utf8StreamParser._matchToken(Utf8StreamParser.java:1188)
 at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:184)
 at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2017)
 at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1974)
 at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1331)
 at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)
 at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:643)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:607)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654)
 at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445)
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)
 at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
 at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
 at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
 at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
 at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
 at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
 at java.lang.Thread.run(Thread.java:662)

我的控制器:

@RequestMapping(value = MappingConstants.RULE_ASSIGNMENT, method = RequestMethod.POST)
 public String saveRuleAssignment(@RequestBody RuleAssignmentCO ruleAssignment) {

  return MappingConstants.REDIRECT + MappingConstants.RULE_ASSIGNMENT;
 }

我的jQuery javascript:

My jQuery javascript:

  ruleAssignment = {
   titleId: titleId,
   typeId: typeId,
   seriesId: seriesId
            }

  jQuery.ajax({
   url: 'ruleAssignment',
   type: 'POST',
   dataType: 'json',
   data: ruleAssignment,
        contentType: 'application/json',
        success: function(result) {
         console.log('here');
        }
  });


推荐答案

jQuery不会自动将Javascript对象编码为JSON,你需要用 JSON.stringify()手动完成:

jQuery doesn't encode Javascript objects into JSON automatically, you need to do it manually with JSON.stringify():

jQuery.ajax({
    url: 'ruleAssignment',
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify(ruleAssignment),
    contentType: 'application/json',
    success: function(result) {
         console.log('here');
    }
});

请注意,某些浏览器不支持 JSON.stringify()作为内置方法,在这种情况下你需要便携性。

Note that some browsers don't support JSON.stringify() as a built-in method, in this case you need json2.js for portability.

这篇关于使用jQuery将JSON对象发布到Spring 3控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:43