我有一个使用Jersey构建的REST服务.
我希望能够根据发送到服务器的MIME设置我的自定义异常编写器的MIME.application/json收到json application/xml时,以及收到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());
}
}
Run Code Online (Sandbox Code Playgroud)
我可以利用什么上下文来获取当前请求Content-Type?
谢谢!
对于任何对完整解决方案感兴趣的人:
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
} …Run Code Online (Sandbox Code Playgroud)