英文:
How to consume a Map with Spring ClientResponse?
问题
首先,我有一个类似这样的 REST URL:
@PostMapping("/check/existence")
@ResponseBody
public Map<String, MyObjectDto> checkExistence() {
//一些代码
}
然后,我有一个使用 Spring WebClient 的消费者,就像这样:
ClientResponse response = webclient.post().uri....
我想要做类似这样的操作:
Map<String, MyObjectDto> responseDto =
response.bodyToMono(Map.class).block();
但是控制台返回了以下信息:
java.util.LinkedHashMap 无法强制转换为 org.mypackage.MyObjectDto
那么,我如何消费一个类似 Map<String, MyObjectDto>
类型的映射呢?
英文:
First, I have a REST URL exposed like that:
@PostMapping("/check/existence")
@ResponseBody
public Map<String, MyObjectDto> checkExistence() {
//some code
then, I have a consumer with a Spring WebClient, like that :
ClientResponse response = webclient.post().uri....
I want to do something like that :
Map<String, MyObjectDto> responseDto =
response.bodyToMono(Map.class).block();
but console returns to me
java.util.LinkedHashMap cannot be cast to org.mypackage.MyObjectDto
so, how can I consume a map typed like Map<String, MyObjectDto>
?
答案1
得分: 6
从类ParameterizedTypeReference<T>
的文档中:
此类的目的是为了能够捕获和传递泛型类型。为了在运行时捕获泛型类型并保留它,您需要创建一个子类(最好是匿名内联类),如下所示:
当您需要将某些东西序列化/反序列化为使用泛型的类型(例如Map<k,v>或List<T>)时,
您不能使用
response.bodyToMono(Map.class)
因为这种方式,Spring不知道您实际上要放入Map中的类型。您会放入int吗?String?对象?它毫无头绪。
因此,您需要提供包含类型信息的内容。
bodyToMono(new ParameterizedTypeReference<Map<String,MyObjectDto>>() {})
ParameterizedTypeReference
是一个匿名类,将为您保存类型信息。因此,该类的作用类似于容器,将类型信息传递给泛型函数bodyToMono
,这样Spring可以查看此对象的内容并确定您要使用的类型。
英文:
From the documentation of the class ParameterizedTypeReference<T>
> The purpose of this class is to enable capturing and passing a generic Type. In order to capture the generic type and retain it at runtime, you need to create a subclass (ideally as anonymous inline class) as follows:
When you need to serialize/deserialize something into a type that uses generics (for example Map<k, v> or List<T>)
You cannot use
response.bodyToMono(Map.class)
Since this way, spring has no idea what types you want to actually put into the Map. Are you going to put in an int? a String? an Object? It has no idea.
so instead you need to supply something that includes the type information.
bodyToMono(new ParameterizedTypeReference<Map<String, MyObjectDto>>() {})
ParameterizedTypeReference
is an anonymous class that will hold your type information for you. So the class acts like a vessel to hold your type information as we pass it into the generic function bodyToMono and that way spring can look at the contents of this object and figure out what types you want to use.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论