1. 注解類
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Pojo { int minLength() default 0; int maxLength() default 0; Class type() default String.class; boolean empty() default true; }
2.Pojo
引入了lombok包
import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * * @ClassName: EBillRecourseBean * @Description:追索功能 vo * @author: zhouyy * @date: 2019年10月9日 下午3:44:11 * */ @Data @NoArgsConstructor @AllArgsConstructor(access = AccessLevel.PUBLIC) public class EBillRecourseBean { private EBillRequestHeader header; // private String testName; // private String testCode; /** 追索通知(bms4ebank0019) **/ @Pojo(empty=false,minLength=20) private String custCmonId;//客戶組織機構代碼 private String custNo;//客戶號 private String custAcct;//客戶賬號 private String custAcctSvcr;//客戶賬號開戶行行號 private String drftNo;//電子票據號碼 private String rcrsDt;//追索申請日期 private String rcrsTp;//追索類型 RT00拒付追索 private String amt;//追索金額 /** 選填 **/ private String rcrsRsnCd;//追索理由代碼 選填 非拒付追索時填寫 【RC00承兌人被依法宣告破產/RC01承兌人因違法被責令終止活動】 /** 選填 **/ private String reqRmrk;//追索通知備注 選填 private String rcvNm;//被追索人名稱 private String rcvAcct;//被追索人賬號 private String rcvAcctSvcr;//被追索人開戶行行號 private String rcvCmonId;//被追索人組織機構代碼 private String eSgnt;//電子簽名 }
3. BaseController
接口需要繼承的controller類
import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; import com.ebilloperate.util.Pojo; public class BaseController { public Map valid(Object bean){ Field[] fileds = bean.getClass().getDeclaredFields(); Map<String,String> map = new HashMap(); map.put("respCode", "0000"); try { for (Field field : fileds) { if(field.isAnnotationPresent(Pojo.class)){ field.setAccessible(true); Pojo annotation = field.getAnnotation(Pojo.class); int maxLength = annotation.maxLength(); int minLength = annotation.minLength(); boolean empty = annotation.empty(); Object value = field.get(bean); Class fieldType = field.getType(); String msg = ""; if(fieldType == String.class){ if(!empty){ if(value == null || "".equals(value.toString().trim())){ msg = field.getName()+"不能為空!"; }else if(maxLength >0 && value.toString().length() >maxLength){ msg = field.getName()+"超長!"; }else if(minLength >0 && value.toString().length() <minLength){ msg = field.getName()+"過短!"; } } if(!"".equals(msg)){ map.put("respMsg", msg); return map; } } } } } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); map.put("respMsg", "接口錯誤"); return map; } return null; } }
4.具體的接口controller類
package com.ebilloperate.features.web.controller; import java.util.HashMap; import java.util.Map; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.common.utils.StringUtil; import com.ebilloperate.features.pojo.EBillRecourseBean; import com.ebilloperate.features.service.EBillRecourseService; import com.bytter.framework.log.FeaturesLog; /** * * @ClassName: EBillRecourceController * @Description:電票追索交易類 * 追索通知 * 同意清償申請 * 同意清償應答(簽收) * @author: zhouyy * @date: 2019年10月9日 下午3:25:47 * */ @RestController @RequestMapping(value="/eBillRecourseTrade") public class EBillRecourseController extends BaseController{ protected FeaturesLog logger = new FeaturesLog(EBillRecourseController.class.getName()); /** * * <p>@Description: 追索通知</p> * @Title NoticeOfRecourse * @author zhouyy * @param bean * @return * @date: 2019年10月9日 下午3:45:33 */ @RequestMapping(value="/test", method=RequestMethod.POST,produces="application/json;charset=UTF-8") public Map test(@RequestBody EBillRecourseBean bean){ Map map = valid(bean); if(map != null){ return map; } //之前要做的 // Map returnMap = eBillRecourseService.doNoticeOfRecourse(bean); Map returnMap = new HashMap(); //之后要做的 returnMap.put("respCode", "0001"); returnMap.put("respMsg", "請求成功!Bytter接口返回隨機字符串:"+UUID.randomUUID().toString().replaceAll("-", "")); if(StringUtil.isBlank(bean.getCustCmonId()) || bean.getCustCmonId().length() >10){ returnMap.put("respCode", "0000"); returnMap.put("respMsg", "請求參數有誤!參數【custCmonId】"); return returnMap; } if(StringUtil.isBlank(bean.getCustNo()) || bean.getCustNo().length() >32){ returnMap.put("respCode", "0000"); returnMap.put("respMsg", "請求參數有誤!參數【custNo】"); return returnMap; } for (Object key : returnMap.keySet()) { System.out.println("key="+key+";value="+returnMap.get(key)); } return returnMap; } }
上面采用的是普通的繼承方法。亦可用spring的aop,在進入controller之前進行校驗,具體的controller就不用繼承、方法中也不需要調用父類方法
5.aop類
package com.ebilloperate.util; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import com.framework.log.FeaturesLog; @Component @Aspect public class ControllerAspect { protected FeaturesLog logger = new FeaturesLog(ControllerAspect.class.getName()); //對包下所有的controller結尾的類的所有方法增強 // private final String executeExpr = "execution(* com.bytter.ebilloperate.features.web..*Controller.*(..))"; private final String executeExpr = "execution(* com.bytter.ebilloperate.features.web..*controller.*(..))"; /** * @param joinPoint: * @throws Exception * @Author: TheBigBlue * @Description: 環繞通知,攔截controller,輸出請求參數、響應內容和響應時間 * @Date: 2019/6/17 * @Return: **/ @Around(executeExpr) public Object processLog(ProceedingJoinPoint joinPoint) throws Exception { Method method = ((MethodSignature) joinPoint.getSignature()).getMethod(); Class returnc = method.getReturnType(); //獲取參數 Object[] args = joinPoint.getArgs(); //接口參數校驗。 必填、長度、格式== for (Object bean : args) { String msg = BytterAnnotation.PojoAnnotation(bean); if(!"".equals(msg)){ Map returnMap = new HashMap(); returnMap.put("respMsg", msg); returnMap.put("respCode", "0000"); return returnMap; } } //獲取方法名稱 String methodName = method.getName(); //獲取參數名稱 // LocalVariableTableParameterNameDiscoverer paramNames = new LocalVariableTableParameterNameDiscoverer(); // String[] params = paramNames.getParameterNames(method); Object resObj = null; try { //執行原方法 resObj = joinPoint.proceed(args); } catch (Throwable e) { logger.error(methodName + "方法執行異常!"); throw new Exception(e); } return resObj; } }
6.注解解析類
package com.ebilloperate.util; import java.lang.reflect.Field; public class BytterAnnotation { public static String PojoAnnotation(Object bean) throws IllegalArgumentException, IllegalAccessException{ Field[] fileds = bean.getClass().getDeclaredFields(); for (Field field : fileds) { if(field.isAnnotationPresent(Pojo.class)){ field.setAccessible(true); Pojo annotation = field.getAnnotation(Pojo.class); int maxLength = annotation.maxLength(); int minLength = annotation.minLength(); boolean empty = annotation.empty(); Object value = field.get(bean); Class fieldType = field.getType(); if(fieldType == String.class){ if(!empty){ if(value == null || "".equals(value.toString().trim())){ return field.getName()+"不能為空!"; } if(maxLength >0 && value.toString().length() >maxLength){ return field.getName()+"超長!"; } if(minLength >0 && value.toString().length() <minLength){ return field.getName()+"過短!"; } } } } } return ""; } }