I know this is not the right place to ask question. but my SO question never got answered.
given a rest api which return json like below :
{
code : '00000',
total: 2
data : [
{},
{},
]
}
java model class of response:
public class SEAReadResponse<T> extends SEAResponse {
private long total;
private List<T> data;
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
}
data is array of arbitrary types, depends on URI, if URI is /user'
, they are json representing User
, if URI is /task
, they are json representing Task
.
I belive it's possible to deserialize such json to SEAReadResponse
with jackson using JavaType
:
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(SEAReadResponse.class, User.class);
SEAReadResponse<User> jsonResponse = objectMapper.readValue(json, javaType);
but with WebClient from WebFlux, I can't find a way to do so:
public <T> List<T> read(SEAReadRequest<T> req) {
ParameterizedTypeReference<SEAReadResponse<T>> typeReference = new ParameterizedTypeReference<SEAReadResponse<T>>() {};
return client.put().uri(req.getLocator()).bodyValue(req).retrieve().bodyToMono(typeReference).block().getData();
}
such code, will cause:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to fwx.apps.pub.models.User
I know generic type T
is erased at runtime, and my code can get entity's class at runtime, but can't find a way to pass it to underlying jackson.
public <T> List<T> read(SEAReadRequest<T> req, Class<T> entityCls) {
// but how can I pass down entityCls to underlying jackon
}
Can I state that the current bodyToMono()
methods limited the usage ?
Comment From: bclozel
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
Additionally, you can link to your SO question here. But creating new issues here to attract more attention to your SO question is not the way to go.
Comment From: WestFarmer
@bclozel If what I am saying is true, it's a enhancement, don't you think so ?
expecting something like:
bodyToMono(JavaType typeRef)