asdf
85
2019-04-16 15:32:20
1
90

(재질문입니다) ajax json 배열 객체를 자바 컨트롤러에서 파라미터를 받고 싶은데요..


다음지도 api 를 사용하여 지도에 다각형을 찍으면 그 다각형 찍은 점(좌표값)을 가지고

(37.64007786203536, 126.92195785432365),(37.64047959659998, 126.93043243865652),(37.63911034060717, 126.93084159585372),(37.63726765370793, 126.92327504265486)

이런 값 형태 입니다

이 좌표값을  JSON.stringify() 를 사용하여 json string 형태로 변환하면

[

  {"ib":126.92195785432365,"jb":37.64007786203536},

  {"ib":126.93043243865652,"jb":37.64047959659998},

  {"ib":126.93084159585372,"jb":37.63911034060717},

  {"ib":126.92327504265486,"jb":37.63726765370793}

]

이런 형태로 변환 됩니다

여기서 ajax를 사용하여

$.ajax({

url : 'jsonAjax.do',

type : 'POST',

datatype : 'json',

data : JSON.stringify(path),

contentType: "application/json; charset=utf-8",

success : function(data){

alert("success==" + data);

console.log("success data = " + data);

},

error : function(request, status, error){

alert("error ==" + error);

console.log("error data = " + data);

}

});

이런식으로 자바 컨트롤러 단으로 넘기고 컨트롤러 단에서

    @SuppressWarnings({ "unchecked", "unused" })

    @RequestMapping(value = "/jsonAjax.do", method = { RequestMethod.POST })

    @ResponseBody

    public void ajaxJsonTest(HttpServletRequest request, @RequestBody String paramData){

System.out.println("@@@@@@@@/jsonAjax.do");

System.out.println("paramData = " + paramData);

        JSONArray jsonArray = JSONArray.fromObject(paramData);

        List<Map<String,Object>> resultMap = new ArrayList<Map<String,Object>>();

        resultMap = JSONArray.fromObject(paramData);

        for (Map<String, Object> map : resultMap) {

        }

    }

이런식으로  구현해서 테스트하면 로그에

2019-04-16 15:25:07,446 DEBUG [org.springframework.web.servlet.DispatcherServlet->doService] DispatcherServlet with name 'action' processing POST request for [/krating/jsonAjax.do]

2019-04-16 15:25:07,446 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping->getHandlerInternal] Looking up handler method for path /jsonAjax.do

2019-04-16 15:25:07,446 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping->getHandlerInternal] Returning handler method [public void krating.common.web.controller.CommonController.ajaxJsonTest(javax.servlet.http.HttpServletRequest,java.lang.String)]

2019-04-16 15:25:07,462 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor->readWithMessageConverters] Reading [class java.lang.String] as "application/json;charset=utf-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@1d865e1]

2019-04-16 15:25:07,479 DEBUG [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver->resolveException] Resolving exception from handler [public void krating.common.web.controller.CommonController.ajaxJsonTest(javax.servlet.http.HttpServletRequest,java.lang.String)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.lang.String out of START_ARRAY token

 at [Source: org.apache.catalina.connector.CoyoteInputStream@1d2466a; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token

 at [Source: org.apache.catalina.connector.CoyoteInputStream@1d2466a; line: 1, column: 1]

2019-04-16 15:25:07,480 DEBUG [org.springframework.web.servlet.DispatcherServlet->processHandlerException] Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name '/common/error/ajaxException'; model is {exceptionMessage=ERROR OCCOR!!!

Could not read JSON: Can not deserialize instance of java.lang.String out of START_ARRAY token

 at [Source: org.apache.catalina.connector.CoyoteInputStream@1d2466a; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token

 at [Source: org.apache.catalina.connector.CoyoteInputStream@1d2466a; line: 1, column: 1]\nCan not deserialize instance of java.lang.String out of START_ARRAY token

 at [Source: org.apache.catalina.connector.CoyoteInputStream@1d2466a; line: 1, column: 1]\n}

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.lang.String out of START_ARRAY token

 at [Source: org.apache.catalina.connector.CoyoteInputStream@1d2466a; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token

 at [Source: org.apache.catalina.connector.CoyoteInputStream@1d2466a; line: 1, column: 1]

at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:181) ~[spring-web-3.2.9.RELEASE.jar:3.2.9.RELEASE]

at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:173) ~[spring-web-3.2.9.RELEASE.jar:3.2.9.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:143) ~[spring-webmvc-3.2.9.RELEASE.jar:3.2.9.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:180) ~[spring-webmvc-3.2.9.RELEASE.jar:3.2.9.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:95) ~[spring-webmvc-3.2.9.RELEASE.jar:3.2.9.RELEASE]


..

..

..

이런식으로 로그내용이 나오는데

 Could not read JSON: Can not deserialize instance of java.lang.String out of START_ARRAY token

이 키워드가 핵심인거 같은데

구글링을 해보고 잇는데 .. 혹시 이 deserialize  역직렬화 관련 아시는분 있을가요..





0
0
  • 답변 1

  • 유리세계
    934
    2019-04-16 16:25:38

    ajax로 데이터 넘기실때

     data : {jsondata : JSON.stringify(path)}

    이렇게 넘겨서 jsondata를 찾아내서 파싱하시는게...

    컨트롤러 첫줄에 프린트는 찍히나요?


    Enumeration paramsss = request.getParameterNames();

    로 request 데이터 꺼내보시면 아무리 JSON.stringify 하셨어도 배열로 처리할 가능성이 크네요


    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.