例外Apper不使用jersey2和spring项目
我们使用Spring和jersey2,I uer ExceptionMapper类实现异常统一处理。但例外情况是,我不工作,希望大家能帮助我。谢谢。 代码示例: 基本例外:例外Apper不使用jersey2和spring项目,spring,jersey,Spring,Jersey,我们使用Spring和jersey2,I uer ExceptionMapper类实现异常统一处理。但例外情况是,我不工作,希望大家能帮助我。谢谢。 代码示例: 基本例外: public class BaseException extends RuntimeException { private static final long serialVersionUID = 1381325479896057076L; private String code; private Object[] v
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;
}
}
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);
}
}
ApiBusinessException:
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;
}
}
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);
}
}
例外情况外观支持:
@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();
}
}
资源示例:
@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配置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>
但是抛出新的ApiBusinessExceptiontest错误。调试不在ExceptionApperSupport类中?嗨,Peesllet,要修改它们是com.ghca.easyview.server.api.resource.FM51包中的映射程序还是子包?如果没有,您可以将包添加到列表中,或者将类直接添加到jersey.config.server.provider.classnames com.ghca.easyview.server.api.resource.FM51包或子包中的映射器,但如果将com.ghca.easyview.server.api.resource.FM51更改为com.ghca.easyview.server.api,仍然不会发生这种情况?。看来这是最基本的包装了谢谢Peesillet