简体   繁体   English

RestTemplate 响应中的错误消息始终为“null”

[英]Error message always "null" from RestTemplate response

I'm losing my mind over here.我在这里失去了理智。 I have 2 servers, one is micro-service behind the scenes and one is the main server that calls it.我有2台服务器,一台是后台的微服务,一台是调用它的主服务器。

In the micro service, I throw an exception that is defined like this:在微服务中,我抛出了一个定义如下的异常:

@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
public class CGTokenException extends Exception{

   // private String msg;

   public CGTokenException() { super("Unknown error getting token / Validating card!"); }

   public CGTokenException(String msg) {super(msg); }
}

In my main server, I use it like this:在我的主服务器中,我这样使用它:

   @Override
    public
    GetPaymentTokenResponse getToken(GetPaymentTokenRequest getPaymentTokenRequest, String url) {

        try {
           String fullPath = url + "/Token/getPaymentToken";
            return this.restTemplate.postForObject(fullPath, getPaymentTokenRequest , GetPaymentTokenResponse.class);
        }
        catch (Exception ex) {
            log.error("Error getting TOKEN / validating card from Credit Guard");
            log.error( "CGTokenController error: " + ex.getMessage() );

            ex.printStackTrace();
        }
}

From inside the microservice I simply do throw new CGTOkenException(msg);从微服务内部我只是throw new CGTOkenException(msg);

For some reason tho, in my main-service logger I get the log of "503 null", like this is the message inside the thrown error (from the line log.error( "CGTokenController error: " + ex.getMessage() ); ).出于某种原因,在我的主服务记录器中,我得到了“503 null”的日志,就像这是抛出的错误中的消息(来自行log.error( "CGTokenController error: " + ex.getMessage() ); ).

But if I send a postman request directly to my microservice- I get the full error with the correct message inside.但是,如果我直接向我的微服务发送 postman 请求,我会收到完整的错误消息,其中包含正确的消息。

what am I doing wrong?我究竟做错了什么? I guess the problem is how I catch it in my main service... but I just can't find what it is.我想问题是我如何在我的主要服务中捕捉到它……但我就是找不到它是什么。 how can I deliver the correct error message to propagate to the main service?如何传递正确的错误消息以传播到主要服务?

You either need to catch the right exception or cast it.您要么需要捕获正确的异常,要么对其进行强制转换。

try {
 String fullPath = url + "/Token/getPaymentToken";
 return this.restTemplate.postForObject(fullPath, getPaymentTokenRequest , GetPaymentTokenResponse.class);
}
catch(HttpClientErrorException hcee) {
  if (hcee.getRawStatusCode() == 503) {
    ...
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用RestTemplate.postForEntity时,始终在ResponseEntity中获得空响应 - Always get null response in ResponseEntity when using RestTemplate.postForEntity Spring 引导 / Mockito:Mocking RestTemplate 但始终响应 Null - Spring Boot / Mockito: Mocking RestTemplate but Response Always Null 反序列化Map时出错 <IgnoredCaseKey, Object> 来自spring restTemplate的回应 - Error while deserializing Map<IgnoredCaseKey, Object> response from spring restTemplate 如何从RestTemplate的响应中检查JSON字符串的错误和成功案例? - How to check response from the RestTemplate for error and success case for the JSON String? 尝试使用MockRestServiceServer模拟RestTemplate,但body始终为null - Trying to mock RestTemplate with MockRestServiceServer but body is always null 来自 JSON 响应的 Json 数组始终为空 - Json array from JSON response is always null 使用AsyncTask来自服务器的第一响应始终为null - First response from server always null with AsyncTask Spring RestTemplate Response值全为null - Spring RestTemplate Response values all null 在 mockito 测试中获取剩余模板交换的 null 响应 - Getting null response of resttemplate exchange in a mockito test 获取 null 值作为 restTemplate 调用中的响应 - Getting null values as response in restTemplate call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM