一个用于统一异常处理的拦截器,当调用有ExceptionProcess声明的方法时将进行处理,否则不处理:
1.spring配置:
<!-- 缓存代理器,aop拦截service -->
<bean id="ExceptionProcessInterceptor" class="com.sifang.app.intercepter.ExceptionInterceptor">
</bean>
<bean id="exceptionProcessProxy"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="ExceptionProcessInterceptor" />
<property name="patterns">
<list>
<value>com.sifang.app.service.*</value>
</list>
</property>
</bean>
<!-- 配合aop拦截器ExceptionProcessInterceptor使用 -->
<aop:aspectj-autoproxy />
2.为了使用<aop:aspectj-autoproxy />需引入包:
<!-- aop -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.2</version>
</dependency>
3.ExceptionInterceptor.java:
package com.sifang.app.intercepter;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import com.sifang.app.annotation.ExceptionProcess;
import com.sifang.app.enums.MessageCode;
import com.sifang.app.util.ResultUtil;
/**
* 异常处理动态代理
*/
public class ExceptionInterceptor implements MethodInterceptor {
//protected final Logger logger = LoggerFactory.getLogger(ExceptionInterceptor.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
//没有ExceptionProcess标识,直接下一步,不做拦截处理
if ( !method.isAnnotationPresent(ExceptionProcess.class)) {
return invocation.proceed();
}
Object result=null;
try {
result = invocation.proceed();
} catch (Exception e) {
return ResultUtil.error(MessageCode.SYS_ERROR.getCode());
}
return result;
}
}
4.JDDQController.java:
package com.sifang.app.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sifang.app.bo.ReturnBo;
import com.sifang.app.bo.JDDQ.DataBo;
import com.sifang.app.service.JDDQService;
@Controller
@RequestMapping(value = "/JDDQ")
public class JDDQController {
@Autowired
public JDDQServicejDDQService;
@ResponseBody
@RequestMapping(value = "/getList", method = RequestMethod.GET)
public String getList(HttpServletResponse response, HttpServletRequest request, ModelMap model) {
//当有异常时将统一处理
ReturnBo<List<DataBo>> result = jDDQService.getList();
return result.toJson();
}
}
5.声明类 ExceptionProcess.java:
package com.sifang.app.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface ExceptionProcess {
}
6.JDDQService.java:
package com.sifang.app.service;
import java.util.List;
import com.sifang.app.annotation.ExceptionProcess;
import com.sifang.app.bo.ReturnBo;
import com.sifang.app.bo.JDDQ.DataBo;
public interface JDDQService {
@ExceptionProcess
public ReturnBo<List<DataBo>> getList();
}