79504704

Date: 2025-03-12 19:47:39
Score: 1
Natty:
Report link

It's incredible that it doesn't work is the base of Generics principle

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))
            .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();
       });

I need to substitute in previous code this line (It's incredible because type must be part of this bean customized dinamically by Spring Framework AOP in the code, I think that a solution for AOP Spring Framework is add a final object Class<T> and in runtime substitute T with correct class passed in constructor by @Autowired annotation as private final value not only verify type class on return):

.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:

    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();
        });
Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @Autowired
  • Low reputation (0.5):
Posted by: SkyBlackHawk