2016-12-15 6 views
1

を使用していない場合は、ここに私の例外のコードは次のとおりです。ませ身体私は春の注釈を使用して例外をスローエンドポイントを持っている春の注釈@ResponseStatus

@ResponseStatus(value = HttpStatus.BAD_REQUEST) 
public class MyException extends BaseApiException{ 

public MyException (String variable){ 

super("variable :"+variable+" can not be updated."); 
} 
} 

私は、RESTエンドポイントをテストするために郵便配達を使用したとき、私正しいステータスコードで正しい結果を得る:

{ 
"errorType": "MyExption", 
"message": "variable : XYZ can not be updated." 
} 

を私は受けて体を受け取らなかったrestTemplateを使用してサービスを呼び出すしようとすると、私の問題があり、ここに私のコードです3210

ResponseEntity<Document> response; 
RestTemplate restTemplate = new RestTemplate(); 
response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entity, Document.class); 

答えて

1

は、あなたはそれが動作します。この

public class MyResponseErrorHandler implements ResponseErrorHandler { 
    private static final Logger log = LoggerFactory.getLogger(MyResponseErrorHandler.class); 

    @Override 
    public void handleError(ClientHttpResponse response) throws IOException { 
     //here you should be able to get //response.getBody() 
     log.error("Response error: {} {}", response.getStatusCode(), response.getStatusText()); 
    } 

    @Override 
    public boolean hasError(ClientHttpResponse response) throws IOException { 
     return isError(response.getStatusCode()); 
    } 

    public static boolean isError(HttpStatus status) { 
     HttpStatus.Series series = status.series(); 
    return (HttpStatus.Series.CLIENT_ERROR.equals(series) 
      || HttpStatus.Series.SERVER_ERROR.equals(series)); 
    } 


} 


RestTemplate template = new RestTemplate(); 
template.setErrorHandler(new MyResponseErrorHandler()); 
+1

感謝を抽出するためにエラーハンドラを定義する必要があります! :) –

関連する問題