依赖注入,拦截@LIst注解类的实现:
package com.yumu.airshow.Config; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.yumu.airshow.Annotation.*; import com.yumu.airshow.Model.Page; import com.yumu.airshow.Utils.ResultUtil; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.validation.ObjectError; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * Created by qa on 19/5/7. */ @Component @Aspect public class ParamValidAspect { @Before("@annotation(paramValid)") public void aroundObject(JoinPoint proceedingJoinPoint , ParamValid paramValid) throws Throwable { Object[] args = proceedingJoinPoint.getArgs(); for (Object argItem : args) { if (argItem instanceof BindingResult) {//前置验证 BindingResult result = (BindingResult) argItem; String errorMap = this.validRequestParams(result); if (errorMap != null) { throw new ValidException(errorMap); } } } } @Around("@annotation(ymInsert)") public Object aroundObject(ProceedingJoinPoint proceedingJoinPoint , YmInsert ymInsert) throws Throwable { return this.around(proceedingJoinPoint,"ymInsert"); } @Around("@annotation(ymUpdate)") public Object aroundObject(ProceedingJoinPoint proceedingJoinPoint , YmUpdate ymUpdate) throws Throwable { return this.around(proceedingJoinPoint,"ymUpdate"); } @Around("@annotation(ymDelete)") public Object aroundObject(ProceedingJoinPoint proceedingJoinPoint , YmDelete ymDelete) throws Throwable { return this.around(proceedingJoinPoint,"ymDelete"); } @Around("@annotation(ymDetail)") public Object aroundObject(ProceedingJoinPoint proceedingJoinPoint , YmDetail ymDetail) throws Throwable { return this.around(proceedingJoinPoint,"ymDetail"); } @Around("@annotation(ymObject)") public Object aroundObject(ProceedingJoinPoint proceedingJoinPoint , YmObject ymObject) throws Throwable { return this.around(proceedingJoinPoint,"ymObject"); } @Around("@annotation(ymList)") public <T>Object aroundList(ProceedingJoinPoint proceedingJoinPoint , YmList ymList) throws Throwable { return this.around(proceedingJoinPoint,"ymList"); } public <T>Object around(ProceedingJoinPoint proceedingJoinPoint , String option) throws Throwable { Object[] args = proceedingJoinPoint.getArgs(); Page page=null; for (Object argItem : args) { System.out.println("argItem:"+argItem); if (argItem instanceof Page) {//分页设置 page = (Page) argItem; System.out.println("getPage_num:"+page.getPage_num()+",getPage_size:"+page.getPage_size()); if(!page.getSort().equals("")) PageHelper.startPage(page.getPage_num(), page.getPage_size(), page.getSort()); else PageHelper.startPage(page.getPage_num(), page.getPage_size()); } if (argItem instanceof BindingResult) {//前置验证 BindingResult result = (BindingResult) argItem; String errorMap = this.validRequestParams(result); if (errorMap != null) { return ResultUtil.errorValidated(errorMap); } } } if(option.equals("ymList")){ List<T> reuslt = new ArrayList<T>(); try { reuslt = (List<T>) proceedingJoinPoint.proceed(); } catch (Exception e) { throw new RuntimeException("返回类型不为List:"+ e.getMessage()); } PageInfo<T> pageList = new PageInfo<T>(reuslt); return ResultUtil.successList(pageList); } Object reuslt = null; try { reuslt = (Object) proceedingJoinPoint.proceed(); } catch (Exception e) { throw new RuntimeException("返回类型不为Object:"+ e.getMessage()); } System.out.println(reuslt); int _reuslt_code=-1; String _reuslt__errorMsg=""; String _reuslt_Msg=""; if (reuslt instanceof Integer) { _reuslt_code=((Integer) reuslt).intValue(); } if (reuslt instanceof String) { _reuslt__errorMsg=reuslt.toString(); } if (reuslt instanceof HashMap) { try { _reuslt_code = Integer.parseInt(((HashMap) reuslt).get("code").toString()); }catch (Exception e){e.printStackTrace();} try { Object msg = ((HashMap) reuslt).get("msg"); if(msg != null){ if(_reuslt_code>0) _reuslt_Msg=msg.toString(); else _reuslt__errorMsg = msg.toString(); } }catch (Exception e){e.printStackTrace();} } if(option.equals("ymDetail"))return ResultUtil.successDetail(reuslt); if(option.equals("ymObject")){ if(_reuslt_code==0 || !_reuslt__errorMsg.equals("")) { if(!_reuslt__errorMsg.equals("")) return ResultUtil.failDelete(reuslt,_reuslt__errorMsg); else return ResultUtil.failDelete(reuslt); }else{ if(!_reuslt_Msg.equals("")) return ResultUtil.successObject(reuslt,_reuslt_Msg); else return ResultUtil.successObject(reuslt); } } if(option.equals("ymDelete")){ if(_reuslt_code==0 || !_reuslt__errorMsg.equals("")) { if(!_reuslt__errorMsg.equals("")) return ResultUtil.failDelete(reuslt,_reuslt__errorMsg); else return ResultUtil.failDelete(reuslt); }else{ if(!_reuslt_Msg.equals("")) return ResultUtil.successObject(reuslt,_reuslt_Msg); else return ResultUtil.successDelete(reuslt); } } if(option.equals("ymUpdate")){ if(_reuslt_code==0 || !_reuslt__errorMsg.equals("")) { if(!_reuslt__errorMsg.equals("")) return ResultUtil.failUpdate(reuslt,_reuslt__errorMsg); else return ResultUtil.failUpdate(reuslt); }else{ if(!_reuslt_Msg.equals("")) return ResultUtil.successObject(reuslt,_reuslt_Msg); else return ResultUtil.successUpdate(reuslt); } } if(option.equals("ymInsert")){ if(_reuslt_code==0 || !_reuslt__errorMsg.equals("")) { if(!_reuslt__errorMsg.equals("")) return ResultUtil.failInsert(reuslt,_reuslt__errorMsg); else return ResultUtil.failInsert(reuslt); }else{ if(!_reuslt_Msg.equals("")) return ResultUtil.successObject(reuslt,_reuslt_Msg); else return ResultUtil.successInsert(reuslt); } } return ResultUtil.errorException(reuslt); } /** * 校验 */ private String validRequestParams(BindingResult result) { if(result.hasErrors()) { List<ObjectError> list = result.getAllErrors(); String errorMsg=""; for (int i = 0; i < list.size(); i++) { FieldError error = (FieldError) list.get(i); errorMsg += error.getField() + error.getDefaultMessage() + ";"; } return errorMsg; } return null; } }
配置pageHelper的相关属性类:
package com.yumu.airshow.Config; import com.github.pagehelper.PageHelper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import java.util.Properties; @Configuration public class PageHelperConfig { @Bean public PageHelper pageHelper() { System.out.println("MyBatisConfiguration.pageHelper()"); PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWithCount", "true"); p.setProperty("reasonable", "true"); pageHelper.setProperties(p); return pageHelper; } }
请求结果重装工具类的实现:
package com.yumu.airshow.Utils; import com.github.pagehelper.PageInfo; import java.util.ArrayList; import java.util.HashMap; /** * Created by qa on 19/5/5. */ public class ResultUtil { /*错误代码一览表 200: ‘服务器成功返回请求的数据。‘,//detail---200,no content data={},list---200, no content data=[] 201: ‘新建或修改数据成功。‘,//ok 202: ‘一个请求已经进入后台排队(异步任务)。‘, 204: ‘删除数据成功。‘,//ok crud出错: 服务器内部错误:500 400: ‘发出的请求有错误,服务器没有进行新建或修改数据的操作。‘, 401: ‘用户没有权限(令牌、用户名、密码错误)。鉴权 ‘, 402: 数据格式: 403: ‘用户得到授权,但是访问是被禁止的。‘, 404: ‘发出的请求针对的是不存在的记录,服务器没有进行操作。‘, 406: ‘请求的格式不可得。‘, 407: ‘手机号码已经存在。‘, 410: ‘请求的资源被永久删除,且不会再得到的。‘, 422: ‘当创建一个对象时,发生一个验证错误。‘, 434: 插入数据失败 434: delete error 435: update error 500: ‘服务器发生错误,请检查服务器。‘, 502: ‘网关错误。‘, 503: ‘服务不可用,服务器暂时过载或维护。‘, 504: ‘网关超时。‘, */ public static Object successInsert(Object object){ HashMap json=new HashMap(); json.put("msg","新增成功!"); json.put("code",200); if(object==null) json.put("data",new HashMap()); else json.put("data",object); return json; } public static Object failInsert(Object object){ return failInsert(object,"新增失败!"); } public static Object failInsert(Object object, String errorMsg){ HashMap json=new HashMap(); json.put("msg",errorMsg); json.put("code",434); if(object==null) json.put("data",new HashMap()); else json.put("data",object); return json; } public static Object successUpdate(Object object){ HashMap json=new HashMap(); json.put("msg","更新成功!"); json.put("code",200); if(object==null) json.put("data",new HashMap()); else json.put("data",object); return json; } public static Object failUpdate(Object object){ return failUpdate(object,"更新失败!"); } public static Object failUpdate(Object object, String errorMsg){ HashMap json=new HashMap(); json.put("msg",errorMsg); json.put("code",435); if(object==null) json.put("data",new HashMap()); else json.put("data",object); return json; } public static Object successDelete(Object object){ HashMap json=new HashMap(); json.put("msg","删除成功!"); json.put("code",200); if(object==null) json.put("data",new HashMap()); else json.put("data",object); return json; } public static Object failDelete(Object object){ return failDelete(object,"删除失败!"); } public static Object failDelete(Object object,String errorMsg){ HashMap json=new HashMap(); json.put("msg",errorMsg); json.put("code",434); if(object==null) json.put("data",new HashMap()); else json.put("data",object); return json; } public static Object successDetail(Object object){ HashMap json=new HashMap(); json.put("msg","获取详情成功!"); json.put("code",200); if(object==null) json.put("data",new HashMap()); else json.put("data",object); return json; } public static Object successObject(Object object){ return failDelete(object,"成功!"); } public static Object successObject(Object object,String errorMsg){ HashMap json=new HashMap(); json.put("msg",errorMsg); json.put("code",200); if(object==null) json.put("data",new HashMap()); else json.put("data",object); return json; } public static Object failObject(Object object){ return failDelete(object,"失败!"); } public static Object failObject(Object object,String errorMsg){ HashMap json=new HashMap(); json.put("msg",errorMsg); json.put("code",403); if(object==null) json.put("data",new HashMap()); else json.put("data",object); return json; } public static Object successList(PageInfo objectPage){ HashMap json=new HashMap(); json.put("msg","获取列表成功!"); json.put("code",200); if(objectPage!=null){ HashMap page=new HashMap(); page.put("count_page", objectPage.getPages()); page.put("count_size", objectPage.getTotal()); page.put("current_page", objectPage.getPageNum()); json.put("data",objectPage.getList()); json.put("page",page); }else{ json.put("data",new ArrayList()); json.put("page",new HashMap()); } return json; } public static Object errorValidated(Object object){ HashMap json=new HashMap(); json.put("msg","数据格式错误!"); json.put("code",402); if(object==null) json.put("data",new ArrayList()); else json.put("data",object); return json; } public static Object failAuthenticate(Object object){ return failDelete(object,"鉴权!"); } public static Object failAuthenticate(Object object,String errorMsg){ HashMap json=new HashMap(); json.put("msg",errorMsg); json.put("code",402); if(object==null) json.put("data",new ArrayList()); else json.put("data",object); return json; } public static Object unLogin(Object object,String errorMsg){ HashMap json=new HashMap(); json.put("msg",errorMsg); json.put("code",499); if(object==null) json.put("data",new ArrayList()); else json.put("data",object); return json; } public static HashMap errorService(String msg){ HashMap json = new HashMap(); json.put("code",407); if(msg.isEmpty()){ json.put("msg","业务错误!"); } else { json.put("msg", msg); } return json; } public static Object errorException(Object object){ HashMap json=new HashMap(); json.put("msg","异常抛错!"); json.put("code",555); if(object==null) json.put("data",new ArrayList()); else json.put("data",object); return json; } }
原文:https://www.cnblogs.com/zhq510/p/11020088.html