2016-09-20 2 views
2

다음과 같은 클래스가 있습니다.Requestbody를 Spring의 JSONObject에 매핑하기

class ExampleBean{ 
    public String Name; 
    public JSONObject data; 
} 

그리고 난 다음과 핸들러 @GET 한 : 내가 JSON을 다음 원하는

@GET 
@Consumes({MediaType.APPLICATION_JSON}) 
public Response getData(ExampleBean dataBean) 
{ 
    // some usage code here 
} 

을 ExmampleBean에 매핑 될 : data는을 인 경우

{ 
    "Name":"Example", 
    "data":{ 
     "hello":"world", 
     "some":"value" 
    } 
} 

모든 것이 완벽하게 작동을 두 개의 공개 필드가 hellosome 인 유형이 있습니다. 그러나 data은 실제로 해당 입력란이나 관련 설정자가없는 JSONObject이므로 들어오는 요청에서 개체를 형성 할 때 데이터 속성을 무시하면 Unrecognized field "hello" (Class JSONObject), not marked as ignorable at [Source: [email protected]; line: 31, column: 18]

+0

'지도 <문자열, 개체> '에서 JSON 객체를 생성? – chrylis

+0

나는 toplevel에'JsonNode'를 성공적으로 매핑했다. jsonnject를 jsonnode로 변경할 때 작동합니까? –

답변

0

이 무시됩니다.

class ExampleBean{ 
    public String Name; 
    @JsonIgnore 
    public JSONObject data; 
} 

나머지 수신 서비스에서 들어오는 요청의 매개 변수로 데이터를 받아들이도록 변경합니다.

@GET 
@Consumes({MediaType.APPLICATION_JSON}) 
public Response getData(@RequestBody ExampleBean dataBean,RequestParam("data") String data) 
{ 

JSONParser parser = new JSONParser(); 
JSONObject json = (JSONObject) parser.parse(data); 
    // some usage code here 
} 

하거나 StringJsonObject data의 데이터 유형을 변경하고 들어오는 요청에서 object을 형성 할 수있다.

class ExampleBean{ 
public String Name; 
public String data; 
} 

나중에는 데이터 문자열

 @GET 
@Consumes({MediaType.APPLICATION_JSON}) 
public Response getData(@RequestBody ExampleBean dataBean) 
{ 

JSONParser parser = new JSONParser(); 
JSONObject json = (JSONObject) parser.parse(dataBean.data); 
    // converting the string data to jsonobject 
} 
+0

나는'@ JsonIgnore'을 시도했다. 일하지 않았어. '@ jsonignore'와는 다른가요? –

+0

죄송합니다. 입력 오류였습니다. 두 번째 방법을 시도해 보셨습니까? – Priyamal

관련 문제