JAX-RS CXF异常包装 [英] JAX-RS CXF Exception wrapping
本文介绍了JAX-RS CXF异常包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想向CXF(2.6.1)添加一个ExceptionMapper,它不仅可以传达响应代码,还可以以有效载荷格式(以我现在使用的JSON形式)运送异常.
I would like to add an ExceptionMapper to CXF (2.6.1) which not only communicates the Response code, but also ships the exception in the payload format (I'm using JSON for now).
@Provider
public class CustomExceptionMapper
implements
ExceptionMapper<MyException>
{
...
@Override
public Response toResponse(MyException mex)
{
//I need something here which can convert mex object to JSON and ship it in response
// I want this to be de-serialized on client
//the following returns the status code
return Response.status(Response.Status.BAD_REQUEST).build();
}
...
}
有没有办法做到这一点?
Is there a way to do this ?
推荐答案
您可能需要使用@Produces将对象序列化为JSON,例如:
You may need to use @Produces to serialize your object to JSON like:
@Produces(MediaType.APPLICATION_JSON)
然后是return Response.ok().entity(OBJECT).build();
测试服务的方式:
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
ClientResponse response = service.path(ADDRESS).type("application/json").get(ClientResponse.class);
String s = response.getEntity(String.class);
System.out.println(s);
private static URI getBaseURI() {
return UriBuilder.fromUri(SERVER ADDRESS).build();
}
这篇关于JAX-RS CXF异常包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文