2017-09-18 7 views
0

をリードし、私は春RestTemplateを使用して応答オブジェクトを取得するために次の行を使用しています:春RestTemplateレスポンスオブジェクトにキャストする前に、応答ステータスをチェックすることがない、適切な変換エラーに

final ResponseEntity<Object> genericErrorResponse = restTemplate 
      .postForEntity("urlvalue.com", request, 
         Object.class); 

私の目標は

if the response is 200: cast to Custom200ResponseModel 

If response is 500: cast to CustomErrorModel 
をチェックすることです

org.springframework.web.client.RestClientException: Could not extract response: 
no suitable HttpMessageConverter found for response type [class java.lang.Object] and content type [text/xml] 

すべての私のmod:

私は、次のエラーが発生しますELSは、それらに@XmlRootElementを持っている - と私はこれを行う最も簡単な方法は何である

response.postForObject(...) 

を使用して直接キャストできますか?

答えて

0

はpostForEntity方法でCustom200ResponseModel.classを使用します。

final ResponseEntity<Custom200ResponseModel> genericErrorResponse = restTemplate 
     .postForEntity("urlvalue.com", request, 
        Custom200ResponseModel.class); 

これはあなたの200応答のために動作します。その後、呼び出しをtry catchでラップし、レスポンスコードに基づいてRestClientResponseExceptionを処理できます。例外のレスポンスコードが500の場合は、CustomErrorModelオブジェクトを手動で作成して設定することができます。

0

さて、レスポンスをStringとして取得し、Jackson ObjectMapperを使用して任意のクラスに変換できます。

関連する問題