programing

jersey2 클라이언트에서 응답으로 목록을 가져오는 방법

fastcode 2023. 3. 6. 21:31
반응형

jersey2 클라이언트에서 응답으로 목록을 가져오는 방법

어떻게 추출할 수 있는지 알고 싶습니다.List<String>의 응답으로서jersey-2.0고객.

이미 해봤는데

List<String> list = client
                      .target(url)
                      .request(MediaType.APPLICATION_JSON)
                      .get(new GenericType<List<String>>(){});

하지만 위의 코드는 작동하지 않습니다.기대했던 것을 반환하지 않습니다.List<String>단, anull대신 값을 매길 수 있습니다.

서비스 응답은 다음과 같이 받을 수 있습니다.Responseclass object를 사용하여 이 오브젝트를 해석합니다.readEntity(...)방법.

다음은 간단한 코드 조각입니다.

List<String> list = client
                      .target(url)
                      .request(MediaType.APPLICATION_JSON)
                      .get(Response.class)
                      .readEntity(new GenericType<List<String>>() {});
/* Do something with the list object */
String listString= serviceResponse.readEntity(String.class);
Gson gson=new Gson();
Type type = new TypeToken<List<String>>(){}.getType();
List<String> list = gson.fromJson(listString, type);

응답 문자열을 가져온 다음 gson 라이브러리를 사용하여 목록으로 변환

1) 에서 응답을 받은 후 readEntity() 메서드를 사용하여 응답 개체를 해석합니다.

List<String> list = client.target(url).
request(MediaType.APPLICATION_JSON).get(Response.class).readEntity(new GenericType<List<String>>() {
});

언급URL : https://stackoverflow.com/questions/35313767/how-to-get-liststring-as-response-from-jersey2-client

반응형