1

我想向 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();
}
...
}

有没有办法做到这一点 ?

4

1 回答 1

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();
}
于 2012-07-27T16:28:45.313 回答