admin管理员组文章数量:1390767
I am getting below response in rest API and want to parse and return the first list item using the spring webclient.
JSON Response
{"_embedded":{"service":[{"id":101,"serviceModelUUID":"aaa8884883","action":"create"},{"id":102,"serviceModelUUID":"aaa8884884","action":"delete"}]}}
I want to return below response
[{"id":101,"serviceModelUUID":"aaa8884883","action":"create"},{"id":102,"serviceModelUUID":"aaa8884884","action":"delete"}]
below is my spring webclient code which is returning single list with null field values
private <T> List<T> getMultipleResources(Class<T> clazz, URI uri) {
return webClient.get().uri(uri).retrieve().bodyToFlux(clazz).collectList().block();
}
I am getting below response in rest API and want to parse and return the first list item using the spring webclient.
JSON Response
{"_embedded":{"service":[{"id":101,"serviceModelUUID":"aaa8884883","action":"create"},{"id":102,"serviceModelUUID":"aaa8884884","action":"delete"}]}}
I want to return below response
[{"id":101,"serviceModelUUID":"aaa8884883","action":"create"},{"id":102,"serviceModelUUID":"aaa8884884","action":"delete"}]
below is my spring webclient code which is returning single list with null field values
private <T> List<T> getMultipleResources(Class<T> clazz, URI uri) {
return webClient.get().uri(uri).retrieve().bodyToFlux(clazz).collectList().block();
}
Share
Improve this question
asked Mar 13 at 8:45
Rajesh KumarRajesh Kumar
3513 silver badges19 bronze badges
1 Answer
Reset to default 1Unpack needed field from your initial response and use constructParametricType
to have JavaType
for a list of instances of the passed Class<T>
A wrapper around the data you need. Here, you extract only the required field (_embedded.service
).
class DataWrapper {
private Object data;
public Object getData() {
return data;
}
@JsonProperty("_embedded")
private void unpackEmbedded(Map<String, Object> embedded) {
this.data = embedded.get("service");
}
}
And construct JavaType
to specify the expected data type returned by the endpoint.
private void howToUse() {
List<Service> res1 = getMultipleResources(URI.create("/"), Service.class);
// prints for your example
// [{id=101, serviceModelUUID=aaa8884883, action=create}, {id=102, serviceModelUUID=aaa8884884, action=delete}]
System.out.println(res1);
// create
System.out.println(res1.getFirst().getAction());
}
private <T> List<T> getMultipleResources(URI uri, Class<T> clazz) {
var wrapperRef = new ParameterizedTypeReference<DataWrapper>() {};
var data = webClient.get().uri(uri).retrieve().bodyToMono(wrapperRef).block().getData();
// better to make an objectMapper as a property of service where getMultipleResources is defined
var objectMapper = new ObjectMapper();
var dataType = objectMapper.getTypeFactory().constructParametricType(List.class, clazz);
return objectMapper.convertValue(data, dataType);
}
Dont fet to define you class - Service
本文标签: how to get the first list item from a json response using the spring webclientStack Overflow
版权声明:本文标题:how to get the first list item from a json response using the spring webclient - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744713118a2621244.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论