我想在CXF(2.6.1)中添加一个ExceptionMapper,它不仅可以传递响应代码,还可以以有效载荷格式发送异常(我现在正在使用JSON)。
@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();
}
...
}
有办法做到这一点吗?
答案 0 :(得分:1)
您可能需要使用@Produces将对象序列化为JSON,如:
@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();
}