2017-09-24 1 views
0

스프링 부트 컨트롤러를 설정하여 들어오는 데이터를 파싱하는 방법에 대한 다른 질문이 있습니다. 배열 객체입니다.스프링 부트는 아약스가있는 콘트롤러에 객체 배열을 보냅니다.

나는 다음 형태로 데이터를 전송하고 있습니다 :

var myData = [ {lang_id : 1, title : "blabla", description : "blablabla"}, {lang_id : 2, title : "der text", description : "der text"}, ... ] 

$.ajax({ 
    url: "/test2", 
    data: JSON.stringify(myData), 
    type: "POST", 
    dataType:"json", 
    contentType:'application/json' 
    ,success : function(res){ 
     console.log("ok!", res.data); 
    } 
    ,error : function(a,b,c){ 
     console.log("err!", a, b, c); 
    } 
}); 

을 다른 답변에 따르면, 난 내 응답 및 응답 래퍼 객체를 만들었습니다.

@PostMapping(value="/test2") 
public @ResponseBody RequestWrapper postIt(@RequestBody RequestWrapper req) { 
    System.out.printf("\n\n%s\n\n", req); 
    return req; 
} 

모든 하나 하나 대답은이 같은 솔루션을 가지고, 아직이 사람이 나를 위해 작동하지 않습니다 다음과 같이

public class Response { 
    private Integer lang_id; 
    private String title; 
    private String description; 
    //getter and setters 
} 

public class RequestWrapper { 
    private List<Response> items; 
    //getters and setters 
} 

그럼, 난 내 컨트롤러를 설정합니다. 다음과 같은 오류가 발생합니다.

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of com.mypackage.controllers.RequestWrapper out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.mypackage.controllers.RequestWrapper out of START_ARRAY token

다른 어떤 시도를 해야할지 모르겠군요.

답변

0

내가 그것을해야한다고 생각 :

public @ResponseBody RequestWrapper postIt(@RequestBody Response[] req) 

RequestWrapper POJO를 감안할 때, 올바른 JSON 양식은 다음과 같습니다

{ 
    "items": [ 
    ... 
    ] 
} 
+0

문제는 내가 지금, 당신의 대답의 두 번째 부분에 수 있었다 내 데이터 가져와. 감사 –

관련 문제