Reputation: 743
I am trying to handle exceptions using the ResponseExceptionMapper class for my cxf client.
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 Client Code:
String url1=
WebClient client = createWebClient(url1).path(/document);
client.headers(someHeaders);
Response response = client.post(byteArry);
For success scenarios, I am getting the correct response code of 200, but for failure scenarios, I never get a response code.
Also is there a better way of handling exceptions in cxf client.
Could someone please help on this.
Upvotes: 0
Views: 1628
Reputation: 39271
How have you registered the ResponseExceptionMapper
to the WebClient?
You need something like this
List<Object> providers = new ArrayList<Object>();
providers.add(new MyServiceRestExceptionMapper()
WebClient client = WebClient.create(url, providers);
I suggest to use a WebApplicationException
insteadof Exception
because default behaviour will raise this kind of exception if no ResponseExceptionMapper
is registered. Also, return the exception, do not throw it. The exception mapper should looks like this.
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;
}
}
Upvotes: 1