2017-03-24 6 views
0

私は、Spring MVCベースのWebサイトを使用して、ユーザーを認証します。サードパーティのAPI(すべてのアプリケーション用のコアユーザーデータベースと通信する)を呼び出す必要があります。 私はこれをうまくやり遂げることができます。何らかの理由でThirdParty APIがダウンしている場合、訪問者がログインまたはサインアップしようとするたびに私のウェブサイトの爆弾も消えてしまいます。スプリングリモート認証中にダウンタイムのシナリオを処理するにはどうすればいいですか?

私はこのようなリモートAPI呼び出しています:ここで

ResponseEntity<LoginApiResponse> responseEntity = restTemplate.exchange(uri, HttpMethod.POST, request, LoginApiResponse.class);

を、LoginApiResponse.classはUserDetailsとそこからの応答が含まれている場合がありますことを、他の情報が含まれていBeanです。

ビーンはおおよそ次のようになります。

public class LoginApiResponse { 
    private HttpStatus status; 
    private String message;  
    private String responseStatus; 
    private UserModel userModel; 
    ... ... 
} 

私は自分のサイト上でエラーを処理CustomRestResponseErrorHandlerを持っています。 しかし、サードパーティのサイトがダウンしている場合、メンテナンスページはどのような種類の要求でも返されるように設定されているため、返信のタイプはtext/htmlで、返答のステータスは200 OKとなります。したがって、ここで例外が発生することはなく、私のCustomRestResponseErrorHandlerは機能しません。むしろ、text/htmlタイプの応答をLoginApiResponseにコールごとに変換できないときにコードが爆発します。

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class org.habba.web.rest.registration.LoginApiResponse] and content type [text/html] 
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109) 
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:811) 
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:795) 
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:574) 
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:547) 
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:468) 

これを回避し、ThirdPartyApiの使用不可能性を早期に判断するにはどうすればよいですか?

答えて

0

APIから受け取った通常の応答のコンテンツタイプがapplication/jsonであると思われるからです。 ResponseEntity<LoginApiResponse>を使用する代わりにResponseEntity<Object>を使用し、responseEntity.getHeaders().getContentType()を使用してコンテンツタイプを確認することができます。コンテンツの種類が期待どおりでない場合は、適切な処置を取ることができます。

+0

与えられたアプローチは私にとってもうまくいきません。同じエラーが表示されます。適切なHttpMessageConverterが見つかりません。 String型を使って試してみましたが、それは私の望むものではありません。 –

関連する問題