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

时间:2020-06-11 11:24:24

标签: java spring-boot resttemplate spring-resttemplate

我在这里迷失了方向。 我有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() );行)。

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

我在做什么错?我想问题是我如何在主要服务中捕获它...但是我只是找不到它。如何传递正确的错误消息以传播到主要服务?

1 个答案:

答案 0 :(得分:0)

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

@ECHO OFF

notepad.exe
REM For the OP's original problem, put drvinst.exe instead of notepad.exe (here and below)

:waitloop
TASKLIST |find /I "notepad.exe" >NUL
IF ERRORLEVEL 1 GOTO endloop
REM echo Notepad running. Waiting 1 second...
timeout /t 1 /nobreak>NUL
goto waitloop
:endloop
echo Done!