接上一篇《dubbox consumer获取provider的exception message》的内容。
使用AOP统一处理一些常用Exception,然后利用ExceptionMapper response数据到consumer
因为要自定义message,所以做的这么麻烦,不然直接全部在ExceptionMappe中处理就行,因为毕竟处理掉的是unchecked exception
//其实感觉需求有点莫名其妙。。
//重新补充,其实consumer真正需要的是checked exception,所以下面的实现对其作用不大。
资料:
http://ugibb510.iteye.com/blog/1762792
http://suntengjiao1.blog.163.com/blog/static/99211088201284112149284/
http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html
http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html
配置文件中增加:
xmlns:aop="http://www.springframework.org/schema/aop"
<pre name="code" class="html">http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
<pre name="code" class="html"><aop:aspectj-autoproxy/>
定义注解:
import java.lang.annotation.*;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
//提供 default 即可在使用是缺失
String DataAccessEM() default "";//DataAccessException 自定义描述
String DuplicateKeyEM() default "";//DuplicateKeyException 自定义描述
String DfaultEM() default "";//Exception 自定义描述
}
定义切面:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
/**
* Created by leedongwei on 15/10/20.
*/
@Aspect
@Component //注入依赖
public class MyAspect {
@AfterThrowing(pointcut="within(com.lee.test..*) && @annotation(ma)", throwing="ex")
public void addMyTest(JoinPoint jp, MyAnnotation ma, Exception ex){
//记录日志
if(ex instanceof DataAccessException){
System.out.println("in DataAccessException");
logger.error(ea.DataAccessEM(), ex.getMessage());
throw new WebApplicationException(ea.DataAccessEM(), Response.Status.BAD_REQUEST);
}else if(ex instanceof DuplicateKeyException){
System.out.println("in DuplicateKeyException");
logger.error(ea.DataAccessEM(), ex.getMessage());
throw new WebApplicationException(ea.DataAccessEM(), Response.Status.CONFLICT);
}else if(ex instanceof WebApplicationException){
System.out.println("in WebApplicationException");
logger.error("WebApplicationException", ex.getMessage());
//donothing
return;
}
System.out.println("in DfaultEM,end");
//除了指定被处理的,其余的都记得记录logger,为后台提供default message 和code
throw new WebApplicationException(ea.DfaultEM(), Response.Status.INTERNAL_SERVER_ERROR);
}
}
配置文件中增加:
<bean id="logAspect" class="com.lee.test.MyAspect">
</bean>
测试:
@MyAnnotation(DataAccessEM = "dataAccessEm lee!",DuplicateKeyEM = "duplicateKeyEm lee!",DfaultEM = "defaultem lee!")
// @MyAnnotation()
public User getUser(@PathParam("id") Long id) {
if (RpcContext.getContext().getRequest(HttpServletRequest.class) != null) {
System.out.println("Client IP address from RpcContext: " + RpcContext.getContext().getRequest(HttpServletRequest.class).getRemoteAddr());
}
if (RpcContext.getContext().getResponse(HttpServletResponse.class) != null) {
System.out.println("Response object from RpcContext: " + RpcContext.getContext().getResponse(HttpServletResponse.class));
}
// int a = 1/0;//Exception test
throw new WebApplicationException("in getUser ex", Response.Status.CONFLICT);
// return userService.getUser(id);
}