繁体   English   中英

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

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

我在这里失去了理智。 我有2台服务器,一台是后台的微服务,一台是调用它的主服务器。

在微服务中,我抛出了一个定义如下的异常:

@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); }
}

在我的主服务器中,我这样使用它:

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

从微服务内部我只是throw new CGTOkenException(msg);

出于某种原因,在我的主服务记录器中,我得到了“503 null”的日志,就像这是抛出的错误中的消息(来自行log.error( "CGTokenController error: " + ex.getMessage() ); ).

但是,如果我直接向我的微服务发送 postman 请求,我会收到完整的错误消息,其中包含正确的消息。

我究竟做错了什么? 我想问题是我如何在我的主要服务中捕捉到它……但我就是找不到它是什么。 如何传递正确的错误消息以传播到主要服务?

您要么需要捕获正确的异常,要么对其进行强制转换。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM