最好的方法是使用ExceptionMapper。您创建一个实现ExceptionMapper的类UnmarshalExceptionMapper。您可以使用“
@Provider”对此进行注释,并在Application构造函数中执行“
classes.add(UnmarshalExceptionMapper.class)”或“ singletons.add(new
UnmarshalExceptionMapper())”。
您的异常映射器将如下所示:
@provider public class UnmarshalExceptionMapper implements ExceptionMapper<UnmarshalException> { public Response toResponse(UnmarshalException exception) { ResponseBuilder rb = Response.status( Response.Status.BAD_REQUEST) // Most appropriate HTTP status pre .entity(your xml goes here) .type("application/xml"); return rb.build(); } }
请注意,您必须将类型设置为“ application /
xml”,因为当前尚未对异常映射器进行内容协商。要进行自己的内容协商,请从请求中获取HttpHeaders,找到“ accept”标头,并相应地设置类型。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)