可以使用ParameterizedTypeReference来避免强制转换。

huangapple 未分类评论42阅读模式
英文:

Can I use ParameterizedTypeReference to avoid cast

问题

我了解如何使用ParameterizedTypeReference来影响RestTemplate的反序列化,但我也希望返回类型能够动态确定。

ParameterizedTypeReference<List<MyModel>> typeRef = new ParameterizedTypeReference<List<MyModel>>() {};

这是我想要重构的代码行:

ResponseEntity<List<MyModel>> = restTemplate.exchange(... typeRef)

是否有一种形式的restTemplate.setExpectedReturnType(refType)

英文:

I understand how to use ParameterizedTypeReference to influence RestTemplate deserialization, however, I would like to also have the return type dynamically determined as well.

ParameterizedTypeReference&lt;List&lt;MyModel&gt;&gt; typeRef = new ParameterizedTypeReference&lt;List&lt;MyModel&gt;&gt;(){};

This the line I'd like to refactor:

ResponseEntity&lt;List&lt;MyModel&gt;&gt; = restTemplate.exchange(... typeRef)

Is there a form of restTemplate.setExpectedReturnType(refType) ?

huangapple
  • 本文由 发表于 2020年7月24日 21:54:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/63075030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定