복붙노트

[SPRING] jQuery를 사용하여 Spring 3 컨트롤러에 JSON 객체 게시하기

SPRING

jQuery를 사용하여 Spring 3 컨트롤러에 JSON 객체 게시하기

JSON 객체를 Spring 3의 컨트롤러에 POST하려고합니다.

다음과 같은 오류가 발생합니다.

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 자바 스크립트 :

  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');
        }
  });

해결법

  1. ==============================

    1.jQuery는 Javascript 객체를 자동으로 JSON으로 인코딩하지 않으므로 JSON.stringify ()를 사용하여 수동으로 처리해야합니다.

    jQuery는 Javascript 객체를 자동으로 JSON으로 인코딩하지 않으므로 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 ()를 내장 메소드로 지원하지 않습니다.이 경우 이동성을 위해 json2.js가 필요합니다.

  2. from https://stackoverflow.com/questions/3983556/using-jquery-to-post-json-object-to-spring-3-controller by cc-by-sa and MIT license