【发布时间】:2013-06-09 09:48:57
【问题描述】:
REST 调用过程中发生异常时,我们需要返回自定义错误代码和错误消息。我们创建了一个异常映射器提供程序,它适用于应用程序代码中的异常。但是,当 CXF 代码发生异常时它不起作用(例如,形成我编写的 CustomValidationInterceptor)。
例如,如果我使用无效的路径参数(例如无效的电话号码)请求。在这种情况下,我们需要以 JSON 格式返回自定义错误代码和错误消息,但即使我们创建了一个异常映射器提供程序来处理 WebApplicationException,它也不起作用。
有什么方法可以处理来自 cxf 拦截器的异常并返回 以类似以下的方式回应用户?
{
"errorDetail": {
"errorCode": "404",
"errorMessage": "Bad Request"
}
}
我的 CustomValidationInterceptor 的代码片段:
public class CustomValidationInterceptor extends AbstractPhaseInterceptor<Message>{
public CustomValidationInterceptor() {
super(Phase.PRE_INVOKE); // Put this interceptor in this phase
}
public void handleMessage(Message message) {
MetadataMap<String, String> metadataMap = (MetadataMap<String, String>) message.get("jaxrs.template.parameters");
if(null != metadataMap) {
List<String> list = metadataMap.get("phoneNumber");
if(null != list) {
String phoneNumber = list.get(0);
boolean result = validatePhoneNumber(phoneNumber);
if(!result){
throw new TelusServiceException(Response.status(Response.Status.BAD_REQUEST).build(), 400, "phone number not valid");
}
} else {
throw new TelusServiceException(Response.status(Response.Status.BAD_REQUEST).build(), 400, "phone number not valid");
}
} else {
throw new TelusServiceException(Response.status(Response.Status.BAD_REQUEST).build(), 400, "phone number not valid");
}
}
public boolean validatePhoneNumber(String phoneNumber) {
Pattern pattern = Pattern.compile("^[1-9]\\d{9}$");
Matcher matcher = pattern.matcher(phoneNumber);
if (!matcher.matches()) {
return false;
}
return true;
}
}
我的自定义异常映射器提供程序的代码片段
public class TelusExceptionHandler implements ExceptionMapper<TelusServiceException> {
public Response toResponse(TelusServiceException exception) {
return Response.status(exception.getErrorDetail().getErrorCode()).entity(exception.getErrorDetail()).build();
}
}
TelusServiceException 的代码片段
public class TelusServiceException extends WebApplicationException{
// constructors and other methods
private ErrorDetail errorDetail = null;
public ErrorDetail getErrorDetail() {
return errorDetail;
}
public void setErrorDetail(ErrorDetail errorDetail) {
this.errorDetail = errorDetail;
}
public TelusServiceException(Response response, int errorCode, String errorMessage) {
super(response);
errorDetail = new ErrorDetail();
errorDetail.setErrorCode(errorCode);
errorDetail.setErrorMessage(errorMessage);
}
}
ErrorDetail 类的代码片段
@XmlRootElement(name="errorDetail")
public class ErrorDetail {
private int errorCode;
private String errorMessage;
@XmlElement(name = "errorCode")
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
@XmlElement(name = "errorMessage")
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
【问题讨论】:
-
我已经更正了 JSON。请看
-
TelusServiceException的.getErrorDetail()的代码是多少? -
添加 TelusServiceException 和 ErrorDetail 对象的代码 sn-p
-
您是否尝试过过滤器,我认为无论 cxf 调用链如何,都会在每个响应中调用它们!
-
我很久以前就为我的一个项目研究过这个问题。下面的答案成功了,所以不知道过滤器是否有用
标签: java exception cxf jax-rs interceptor