我们使用Spring和jersey2,我的ExceptionMapper类实现异常统一处理。但ExceptionMapper不起作用,希望大家帮助我。谢谢 代码示例:
BaseException:
public class BaseException extends RuntimeException {
private static final long serialVersionUID = 1381325479896057076L;
private String code;
private Object[] values;
public String getCode() {
return code;
}
public BaseException() {
}
public BaseException(String message) {
super(message);
}
public BaseException(String message, Throwable e) {
super(message, e);
}
public BaseException(String message, Throwable cause, String code,Object[] values) {
super(message, cause);
this.code = code;
this.values = values;
}
}
ApiBusinessException:
public class ApiBusinessException extends BaseException{
/**
*
*/
private static final long serialVersionUID = 6369280619686286597L;
public ApiBusinessException() {
super();
}
public ApiBusinessException(Throwable cause, String code) {
super(code, cause, code, null);
}
public ApiBusinessException(String code, Object[] values) {
super(code, null, code, values);
}
public ApiBusinessException(String message, Throwable cause, String code,Object[] values) {
super(message, cause, code, values);
}
public ApiBusinessException(String message, Throwable e) {
super(message, e);
}
public ApiBusinessException(String message) {
super(message);
}
}
ExceptionMapperSupport:
@Provider
@Component
public class ExceptionMapperSupport implements ExceptionMapper<ApiBusinessException> {
private static final String CONTEXT_ATTRIBUTE = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
@Context
private HttpServletRequest request;
@Context
private ServletContext servletContext;
@Override
public Response toResponse(ApiBusinessException e) {
String localemessage = e.getMessage();
String message = e.getMessage();
String code = Status.INTERNAL_SERVER_ERROR.getStatusCode() + "";
Status statusCode = Status.INTERNAL_SERVER_ERROR;
WebApplicationContext context = (WebApplicationContext) servletContext.getAttribute(CONTEXT_ATTRIBUTE);
// unchecked exception
if (e instanceof BaseException) {
BaseException baseException = (BaseException) e;
code = baseException.getCode();
Object[] args = baseException.getValues();
localemessage = context.getMessage(code, args, e.getMessage(),request.getLocale());
} else{
localemessage = context.getMessage(code, null, e.getMessage(),request.getLocale());
code = Status.NOT_FOUND.getStatusCode() + "";
statusCode = Status.NOT_FOUND;
}
JSONObject jso = new JSONObject();
jso.put("code", code);
jso.put("message", message);
jso.put("localemessage", localemessage);
LogUtils.logError(message, e);
return Response.status(statusCode).entity(jso.toJSONString()).type(MediaType.APPLICATION_JSON).build();
}
}
ResourceExample:
@Component
@Path("/tool/cloud/vrm/{platform_type}")
@Api(value = "/apps", description = "")
@Produces({ "application/json"})
public class AppInstanceResource extends BaseResource {
@POST
@Path("/vdcs/{vdc_id}/vpc/{vpc_id}/user/apps")
public Response createUserAppInstances(
@ApiParam(value = "", name = PLATFORM_TYPE, required = true) @PathParam(PLATFORM_TYPE) String platformType,
@ApiParam(value = "", name = PLATFORM_ID, required = true) @NotNull@QueryParam(PLATFORM_ID) String platformId,
@ApiParam(value = "", name = AUTH_USER_ID, required = true) @NotNull@HeaderParam(AUTH_USER_ID) String authGhcaUserId,
@ApiParam(value = "VDC ID", name = VDC_ID, required = true) @PathParam(VDC_ID) String vdcId,
@ApiParam(value = "VPC ID", name = VPC_ID, required = true) @PathParam(VPC_ID) String vpcId,
@ApiParam(value = "", name = CLOUD_INFRAS_ID, required = true)@QueryParam(CLOUD_INFRAS_ID) String cloudInfrasId,
@ApiParam(value = BODY, name = "createUserAppInstances", required = true) @Valid @NotNull CreateUserAppInstancesModel createUserAppInstances) throws ApiBusinessException{
Response response = null;
Map<String, Object> headParams = Maps.newHashMap();
headParams.put(TASK_PLATFORM_ID, platformId);
headParams.put(TASK_PLATFORM_TYPE, platformType);
headParams.put(TASK_AUTH_USER_ID, authGhcaUserId);
headParams.put(TASK_VDC_ID, vdcId);
headParams.put(TASK_VPC_ID, vpcId);
headParams.put(TASK_CLOUD_INFRAS_ID, cloudInfrasId);
String currentResourceName = "create apps";
String bodyData = JSON.toJSONString(createUserAppInstances);
LogBeanLocal.valueOf(currentResourceName, bodyData);
String returnData = service.createBasic(bodyData, headParams, false, null, CREATE_USERAPPINSTANCE, currentResourceName, null);
response = processResponseData(returnData, currentResourceName);
throw new ApiBusinessException("test error.");
//return responseTo(response);
}
}
Jersey Config xml:
<servlet>
<!-- jersey ServletContainer -->
<servlet-name>jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- jersey resource packages-->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.wordnik.swagger.jaxrs.json,
com.ghca.easyview.server.api.resource.FM51
</param-value>
</init-param>
<!-- jersey provider classnames-->
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
com.wordnik.swagger.jersey.listing.ApiListingResourceJSON,
com.wordnik.swagger.jersey.listing.JerseyApiDeclarationProvider,
com.wordnik.swagger.jersey.listing.JerseyResourceListingProvider
</param-value>
</init-param>
<!-- <init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.ghca.easyview.server.api.filter</param-value>
</init-param> -->
<init-param>
<param-name>jersey.config.server.wadl.disableWadl</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.ghca.easyview.server.api.context.JerseyApplication</param-value>
</init-param>
<!-- jersey beanValidation -->
<init-param>
<param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/se/rest/*</url-pattern>
</servlet-mapping>
但抛出新的ApiBusinessException(&#34;测试错误。&#34;)Debug不在ExceptionMapperSupport类中?