2017-01-28 1 views
1

예제라는 사용자 정의 객체가 있습니다. 여기에는 mongo 질의로서 org.json.JSONObject가 있습니다.Spring @RequestBody가 사용자 정의 객체에 매핑되지 않습니다.

@RestController 
@RequestMapping("/example") 
public class ExampleRestController { 

@RequestMapping(value = "/search", method = RequestMethod.POST) 
@ResponseBody 
public String example(@RequestBody ExampleObject example) { 

    return "This is an example"; 
} 

을 그리고, 나는 우체부와 함께 다음과 같은 요청을 수행합니다 :

public class ExampleObject { 

private JSONObject query; 
private String[] projections; 
private Number limit; 

// Constructors, getters and setters 

} 

나는이 같은 REST 컨트롤러를 사용하고

POST

http://localhost:8080/example/search로를 본문은 다음과 같습니다 (나는와 JSON의 유효성을 확인했다. 15,) :

{ 
"query":{ 
    "field1":"value1", 
    "field2":"value2" 
    }, 
"projections":["field3, field4"], 
"limit":3 
} 

결과는 다음과 같습니다 객체 "예"에 대한 계획과 한계는 제대로 잘 살고했지만 쿼리) (null이 아닌 빈 된 JSONObject입니다. 필드 쿼리를 보내지 않으면 객체 "example"의 JSONObject 변수가 null입니다.

필자는 필드 쿼리가 제대로 설정되지 않은 이유를 알지 못합니다. Spring에 @RequestBody json을 ExampleObject에 매핑하고 싶습니다.

+1

잭슨이'JSONObject'를 비 직렬화하는 방법을 모른다고 의심하고 있습니다. 'com.fasterxml.jackson.core.JsonNode'로 시도해 볼 수 있습니까? –

+0

JsonNode로 잘 작동합니다! 감사!! –

답변

1

스프링 (특히 Jackson)은 JSONObject을 역 직렬화하는 방법을 알지 못합니다. 대신 com.fasterxml.jackson.core.JsonNode을 사용할 수 있습니다.

관련 문제