Home > Blockchain >  Java Supplier<> get origins information
Java Supplier<> get origins information

Time:11-08

I Use the Supplier in my code to call restTemplate and make the custom Message when have exception..

But, im my message, i need get information by my requestCall, But when i cast the request the java thow error


...

My code:

public void execute() {
    HttpHeaders headers = buildDefaultHeaders();
    UriBuilder uri = UriBuilder.fromUri(wdd3dGatewayEndpoint   API_URL);
    HttpEntity request = new HttpEntity(headers);
    this.executeRequest(() -> restTemplate.exchange(uri.build(), HttpMethod.DELETE, request, Void.class));
}

My Supplier

protected ResponseEntity executeRequest(Supplier<ResponseEntity> request) {
    try {
        ResponseEntity response = request.get();
        updateSessionToken(response);
        return response;
    } catch (HttpClientErrorException | HttpServerErrorException e) {
        String msg = "WDD3D-Error in service communication<br>"   e.getResponseBodyAsString();
        throw new MaestroException(msg);
    }
}

Now, i try cast to get URL...

protected ResponseEntity executeRequest(Supplier<ResponseEntity> request) {
    try {
        ResponseEntity response = request.get();
        updateSessionToken(response);
        return response;
    } catch (HttpClientErrorException | HttpServerErrorException e) {

        //THROW EXEPTION HERE... PLEASE HELP...
        RequestEntity requestEntity = (RequestEntity) request;
        
        String url = requestEntity.getUrl().toString();
        String msg = "WDD3D-Error in service communication<br>"   e.getResponseBodyAsString();
        throw new MaestroException(msg);
    }
}]

CodePudding user response:

You should use the get() method of the Supplier, see more in the docs.

CodePudding user response:

    RequestEntity requestEntity = (RequestEntity) request;

You are trying to cast a Supplier<ResponseEntity> to a RequestEntity. These are two very different classes and such a cast will never work.

Maybe you want to call request.get() and get the URL from the ResponseEntity that you have.

Tell me if it works for you in the comments or we need to debug further ?

CodePudding user response:

The only thing you are trying to get from the RequestEntity is the URL, which you can't get from the Supplier<ResponseEntity> since it is not a RequestEntity, so why not just pass the URL as another parameter to executeRequest? Then it would have the additional information it needs to log the error.

  • Related