无法获得通用的ResponseEntity< T>其中T是泛型类“SomeClass< SomeGenericType>” [英] Unable to get a generic ResponseEntity<T> where T is a generic class "SomeClass<SomeGenericType>"
问题描述
请帮我获得 ResponseEntity< T>
,其中 T
本身就是一个通用类型。正如我现在看到的那样,现在春天RestTemplate不支持这个功能。我使用的是Spring MVC 3.1.2版本。
这里是我的代码,我想使用:
代码:
ResponseEntity< CisResponse< CisResponseEntity>> res =
this.restTemplate.postForEntity(
this.rootURL,myRequestObj,CisResponse.class);
我得到这个错误:
类型不匹配:无法从ResponseEntity转换< CisResponse> to
ResponseEntity< CisResponse< CisResponseEntity>>
显而易见的错误,但我今天能如何解决它?
比我想得到我的通用响应类型:
CisResponse< CisResponseEntity> myResponse = res.getBody();
CisResponseEntity entity = myResponse.getEntityFromResponse();
现在,我使用这个解决方案,使用 postForObject()
postForEntity():
CisResponse< CisResponseEntity> ; response =
this.restTemplate.postForObject(
this.rootURL,myRequestObj,CisResponse.class);
这是一个已知问题。现在通过引入 ParameterizedTypeReference
来解决它,它是一个参数化类型,您明确地继承以在运行时提供类型信息。这称为超类型令牌,并且围绕类型擦除工作,因为子类(在本例中为匿名)在运行时保留通用超类型的类型参数
但是,您不能使用 postForObject
,因为API仅支持 交换()
:
ResponseEntity< CisResponse< CisResponseEntity>> res = template.exchange(
rootUrl,
HttpMethod.POST,
null,
new ParameterizedTypeReference< CisResponse< CisResponseEntity>>(){});
请注意,最后一行显示了超级类型令牌:您不提供文字 CisResponse.class
,但是匿名实例化参数化类型 ParameterizedTypeReference< T>
,它可以在运行时期望提取子类型信息。您可以将超类型令牌视为 hacks ,以实现 Foo< Bar< Baz>> .class
$如果你的对象定义了一个
url BTW,那么在Java中,你不需要用这个
前缀来访问实例变量: / code>和模板
成员,只需以简单的名称访问它们,而不是像 this.url
和 this.template
Please help me to get a ResponseEntity<T>
where T
is itself a generic type. As I see it of now, this is not supported nowdays by spring RestTemplate. I'm using Spring MVC version 3.1.2
Here is my code, that I want to use:
Code:
ResponseEntity<CisResponse<CisResponseEntity>> res =
this.restTemplate.postForEntity(
this.rootURL, myRequestObj, CisResponse.class);
I'm getting this error:
Type mismatch: cannot convert from ResponseEntity<CisResponse> to
ResponseEntity<CisResponse<CisResponseEntity>>
It's obvious error, but how I can workaround it today?
Than I do want to get my generic response type:
CisResponse<CisResponseEntity> myResponse= res.getBody();
CisResponseEntity entity = myResponse.getEntityFromResponse();
For now, I use this solution, with postForObject()
and not postForEntity()
:
CisResponse<CisResponseEntity> response =
this.restTemplate.postForObject(
this.rootURL,myRequestObj, CisResponse.class);
解决方案 This was a known issue. Now it's fixed with the introduction of ParameterizedTypeReference
, which is a parameterized type that you explicitely inherit to supply type information at runtime. This is called a super-type token, and works around type erasure because subclasses (anoniymous in this case) keep the type arguments of the generic supertype at runtime.
However you can't use postForObject
, because the API only supports exchange()
:
ResponseEntity<CisResponse<CisResponseEntity>> res = template.exchange(
rootUrl,
HttpMethod.POST,
null,
new ParameterizedTypeReference<CisResponse<CisResponseEntity>>() {});
Note that the last line demonstrates the idea of super type tokens: you don't supply the literal CisResponse.class
, but an anonymous instantiation of the parameterized type ParameterizedTypeReference<T>
, which at runtime can be expected to extract subtype information. You can think of super type tokens as hacks for achieving Foo<Bar<Baz>>.class
BTW, in Java you don't need to prefix access to instance variable with this
: if your object defines a url
and template
members, just access them with their simple name, and not by prefixing like you do this.url
and this.template
这篇关于无法获得通用的ResponseEntity< T>其中T是泛型类“SomeClass< SomeGenericType>”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!