Java 带有XML或JSON的JAX-RS(Jersey)自定义异常
我有一个用球衣做的休息服务 我希望能够根据发送到服务器的MIME设置自定义异常编写器的MIME<收到json时返回code>application/json,收到xml时返回Java 带有XML或JSON的JAX-RS(Jersey)自定义异常,java,mime,jersey,jax-rs,Java,Mime,Jersey,Jax Rs,我有一个用球衣做的休息服务 我希望能够根据发送到服务器的MIME设置自定义异常编写器的MIMEapplication/json,收到xml时返回application/xml 现在我硬编码application/json,但这让XML客户机蒙在鼓里 public class MyCustomException extends WebApplicationException { public MyCustomException(Status status, String message,
application/xml
现在我硬编码application/json
,但这让XML客户机蒙在鼓里
public class MyCustomException extends WebApplicationException {
public MyCustomException(Status status, String message, String reason, int errorCode) {
super(Response.status(status).
entity(new ErrorResponseConverter(message, reason, errorCode)).
type("application/json").build());
}
}
我可以点击什么上下文来获取当前请求内容类型
谢谢
根据答案更新 对于对完整解决方案感兴趣的任何其他人:
public class MyCustomException extends RuntimeException {
private String reason;
private Status status;
private int errorCode;
public MyCustomException(String message, String reason, Status status, int errorCode) {
super(message);
this.reason = reason;
this.status = status;
this.errorCode = errorCode;
}
//Getters and setters
}
连同一个例外标记
@Provider
public class MyCustomExceptionMapper implements ExceptionMapper<MyCustomException> {
@Context
private HttpHeaders headers;
public Response toResponse(MyCustomException e) {
return Response.status(e.getStatus()).
entity(new ErrorResponseConverter(e.getMessage(), e.getReason(), e.getErrorCode())).
type(headers.getMediaType()).
build();
}
}
@Provider
公共类MyCustomExceptionMapper实现ExceptionMapper{
@上下文
私有HttpHeader;
公众响应(MyCustome例外){
返回Response.status(例如getStatus())。
实体(新的ErrorResponseConverter(e.getMessage()、e.getReason()、e.getErrorCode())。
类型(headers.getMediaType()。
build();
}
}
如果ErrorResponseConverter是自定义JAXB POJO,您可以尝试将@javax.ws.rs.core.Context javax.ws.rs.core.HttpHeaders字段/属性添加到根资源类、资源方法参数或自定义javax.ws.rs.ext.ExceptionMapper,并调用HttpHeaders.getMediaType()。
public Response toResponse(final CustomException e) {
LOGGER.debug("Mapping CustomException with status + \"" + e.getStatus() + "\" and message: \"" + e.getMessage()
+ "\"");
ResponseBuilder rb = Response.status(e.getStatus()).entity(
new ErrorResponseConverter(e.getMessage(), e.getReason(), e.getErrorCode()));
List<MediaType> accepts = headers.getAcceptableMediaTypes();
if (accepts!=null && accepts.size() > 0) {
//just pick the first one
MediaType m = accepts.get(0);
LOGGER.debug("Setting response type to " + m);
rb = rb.type(m);
}
else {
//if not specified, use the entity type
rb = rb.type(headers.getMediaType()); // set the response type to the entity type.
}
return rb.build();
}
公众响应(最终客户例外){
LOGGER.debug(“将CustomException映射为状态+\”“+e.getStatus()+”\”和消息:\”“+e.getMessage()
+ "\"");
ResponseBuilder rb=Response.status(e.getStatus()).entity(
新的ErrorResponseConverter(e.getMessage(),e.getReason(),e.getErrorCode());
List accepts=headers.getAcceptableMediaTypes();
if(accepts!=null&&accepts.size()>0){
//就挑第一个吧
MediaType m=accepts.get(0);
LOGGER.debug(“将响应类型设置为“+m”);
rb=rb.类型(m);
}
否则{
//如果未指定,请使用实体类型
rb=rb.type(headers.getMediaType());//将响应类型设置为实体类型。
}
返回rb.build();
}
ExceptionMapper是一个不错的选择。谢谢代替:公共类MyCustomException扩展WebApplicationException,我创建了一个:公共类MyCustomException扩展RuntimeException和一个:公共类MyCustomExceptionMapper实现了ExceptionMapper,该类实现了:public Response to Response(PlacesException e){return Response.status(e.getStatus()).entity(新的ErrorResponseConverter(e.getMessage()、e.getReason()、e.getErrorCode())).type(headers.getMediaType()).build();}ErrorResponseConverter类是什么样子的?@Oskar:请向我们展示您的ErrorResponseConverter实现。谢谢@dreboy它只是一些POJO,将返回给包含错误信息的用户。您可以为Jackson/JAXB/which添加注释,以支持各种内容类型。@ach我找到了它。谢谢你的回复!我同意您应该使用Accept:header,而不是HttpHeaders.getMediaType()。我认为模棱两可的只是选择第一个。对我来说,尝试使用与Jersey为请求匹配的资源的@products注释相对应的注释更有意义。然而,在我的脑海里,我不知道如何把它联系起来。@dominic-d实际上,在这个主题上似乎有一个开放的JIRA-因此,鉴于此,我想说你的解决方案是目前你能做的最好的。