2016-10-31 6 views
0

私はcxfクライアントのResponseExceptionMapperクラスを使用して例外を処理しようとしています。cxfクライアントのResponseExceptionMapper

ExceptionHandlingCode:

public class MyServiceRestExceptionMapper implements ResponseExceptionMapper<Exception> { 


private static final Logger LOGGER = LoggerFactory.getLogger(MyServiceRestExceptionMapper .class); 

public MyServiceRestExceptionMapper() { 
} 

@Override 
public Exception fromResponse(Response response) { 

    LOGGER.info("Executing MyServiceRestExceptionMapper class"); 

    Response.Status status = Response.Status.fromStatusCode(response.getStatus()); 

    LOGGER.info("Status: ", status.getStatusCode()); 

    switch (status) { 

     case BAD_REQUEST: 
      throw new InvalidServiceRequestException(response.getHeaderString("exception")); 

     case UNAUTHORIZED: 
      throw new AuthorizationException(response.getHeaderString("exception")); 

     case FORBIDDEN: 
      throw new AuthorizationException(response.getHeaderString("exception")); 

     case NOT_FOUND: 
      throw new 
        EmptyResultDataAccessException(response.getHeaderString("exception")); 

     default: 
      throw new InvalidServiceRequestException(response.getHeaderString("exception")); 

    } 

} 

} 

CXFクライアントコード:成功のシナリオについては

String url1= 
WebClient client = createWebClient(url1).path(/document); 
client.headers(someHeaders); 
Response response = client.post(byteArry); 

、私は200の正しい応答コードを取得していますが、障害が発生した場合のために、私は応答を取得することはありませんコード。

また、cxfクライアントで例外を処理する優れた方法があります。

誰か助けてもらえますか?

答えて

1

ResponseExceptionMapperをWebClientにどのように登録しましたか?

あなたはこの

List<Object> providers = new ArrayList<Object>(); 
providers.add(new MyServiceRestExceptionMapper() 
WebClient client = WebClient.create(url, providers); 

ようなものが必要私はResponseExceptionMapperが登録されていない場合、デフォルトの動作は、例外のこの種を発生させますので、Exception insteadof WebApplicationExceptionを使用することをお勧めします。また、例外を返し、投げないでください。例外マッパーは次のようになります。

public class MyServiceRestExceptionMapper implements ResponseExceptionMapper<WebApplicationException> 

    public MyServiceRestExceptionMapper() { 
    } 

    @Override 
    public WebApplicationException fromResponse(Response response) { 
     //Create your custom exception with status code 
     WebApplicationException ex = ... 

     return ex; 
    } 
} 
関連する問題