-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
It's incredible that it doesn't work.
Is it a bug or a feature that nobody have implemented in Spring Framework?
It's the base of Generics principle (Below, I propose a solution to solve it after creation object you have the type so inject after construction object Type as private and final in object as Class).
I have a generics class <S,T> where S is a request body and T is response body of a webclient in a reactive mode get new Bearer Token or return latest token, if it doens't expried, and calling server by that token without using .block*() methods in no blocking mode.
In my class I need to use my class which is Client<S, T> and a method return T type responsebody and pass requestbody parameter as S type but using T in below and it doesn't work:
public Mono<T> getResponse( @NotBlank String logHash,
@NotBlank String url,
@NotNull @NotBlank @NotEmpty S requestBody,
@NotNull @NotBlank @NotEmpty Class<T> responseBodyClass) {
return getToken()
.flatMap(token ->
webClient
.post()
.uri(url)
.header("Authorization", "Bearer " + token)
.body(BodyInserters.fromValue(requestBody))
.retrieve()
.onStatus(HttpStatus.INTERNAL_SERVER_ERROR::equals, response -> response.bodyToMono(String.class).map(Exception::new))
.onStatus(HttpStatus.BAD_REQUEST::equals, response -> response.bodyToMono(String.class).map(Exception::new))
THE PROBLEM ! ========> .bodyToMono(new ParameterizedTypeReference<T>() {})
.doOnSuccess(response -> log.debug("{} full url {} response {}", logHash, this.serverUrl + url, response))
.doOnError(ex -> {
log.error("{} {}", logHash, ex.getMessage());
ex.printStackTrace();
})
)
.doOnSuccess(token -> log.debug("{} url {} response {}", logHash, this.serverUrl + url, token))
.doOnError(ex -> {
log.error("{} {}", logHash, ex.getMessage());
ex.printStackTrace();
});
.bodyToMono(new ParameterizedTypeReference<T>() {})
with responseBody Class Type which is a .class of my responseBody to avoid exception because JVM can't find T type for return body object:
.bodyToMono(responseBody)
What type of Generics Type is this implementatio?I autowire class I pass Type in autowiring how is possibile that this can't find Type?
new ParameterizedTypeReference<T>() {}
caller without use @Autowired as best practices:
private Client<CheckCustomerBody, ApimResponse> client = null;
And in method that use client it I need to passawhen call it
client = new Client<CheckCustomerBody, ApimResponse>();
client.configure( WebClient.builder(),
ClientType.XXXX_XX,
channelEnum,
logHash,
Optional.empty(),
Optional.empty(),
Optional.empty()
);
return client
.getResponse( logHash,
WebOrderConstants.API_EXT_CHECK,
request,
ApimResponse.class)
.map(apimResponse -> validationProductChange(
apimResponse.getResponse(),
customer.getClientAccountCode(),
logHash
)
? "OK"
: "KO"
)
.doOnError(ex -> {
log.error("{} END {} : Error using d365 service", logHash, prefix);
ex.printStackTrace();
});
Thanks a lot for your reply, I searched a different solution but I can't find it and I don't know why this is normal in AOP of Spring, have Type in Generics class can find a lot of exceptions.
Please analyze this scenario but in general will be very usefull allow to inject by Spring Framework AOP the type of Generics when object is created to analyze in runtime if there are errors and without add type strictly in class.
Spring Team can use the same logic of @value used for application.properties but in runtime you have Type when istantiate the class or using @PostConstruct.