RESTEasy 클라이언트를 사용하여 API에서 JSON 문자열을 검색하고 있습니다.RESTEasy 클라이언트에서 JSON 응답의 특정 노드 추출
{
"foo1" : "",
"foo2" : "",
"_bar" : {
"items" : [
{ "id" : 1 , "name" : "foo", "foo" : "bar" },
{ "id" : 2 , "name" : "foo", "foo" : "bar" },
{ "id" : 3 , "name" : "foo", "foo" : "bar" },
{ "id" : 4 , "name" : "foo", "foo" : "bar" }
]
}
}
가 지금은 객체 매핑만을 items
노드를 추출하고 싶습니다 다음 JSON 페이로드는 다음과 같이 보인다. JSON 응답 본문을 가로 채고 루트 노드로 items
이되도록 수정하는 가장 좋은 방법은 무엇입니까?
내 API 메소드에 RESTEasy proxy framework을 사용하고 있습니다.
나머지 클라이언트 코드 :
ResteasyWebTarget target = client.target("https://"+server);
target.request(MediaType.APPLICATION_JSON);
client.register(new ClientAuthHeaderRequestFilter(getAccessToken()));
MyProxyAPI api = target.proxy(MyProxyAPI.class);
MyDevice[] result = api.getMyDevice();
RESTEasy가 프록시 인터페이스 :
public class CustomReaderInterceptor implements ReaderInterceptor {
private ObjectMapper mapper = new ObjectMapper();
@Override
public Object aroundReadFrom(ReaderInterceptorContext context)
throws IOException, WebApplicationException {
JsonNode tree = mapper.readTree(context.getInputStream());
JsonNode items = tree.get("_bar").get("items");
context.setInputStream(new ByteArrayInputStream(mapper.writeValueAsBytes(items)));
return context.proceed();
}
}
이 그 다음을 등록 : 당신은 당신의 JSON을 조작 할 수 ReaderInterceptor
을 만들고 잭슨을 사용할 수
public interface MyProxyAPI {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/device")
public MyDevice[] getMyDevices();
...
}
는 자바 스크립트 객체로 변환하고 객체로 검색 할 수 있습니다. – SaviNuclear
'items'가 루트가되는 것이 중요합니까 아니면 메시지의 다른 요소를 매핑하고 싶지 않은 것입니까? RestEasy 문서를 잠시 살펴보면 JAXB 공급자를 통해 Java Bean에 자동 정렬/비 정렬을 사용하고있는 것입니까? – dbreaux
@ dbreaux 네, 다른 요소를 매핑하고 싶지는 않습니다. 간단한 구현 코드는 OP 편집을 참조하십시오. 물론 메시지 본문을 변경하는 공급자를 등록 할 수는 있지만 그런 공급자의 구현이 어떻게 생겼는지 궁금합니다. –