spring 接口校验参数(自定义注解)
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 "";
}
}
spring 接口校验参数(自定义注解)的更多相关文章
- 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- Spring中如何使用自定义注解搭配@Import引入内外部配置并完成某一功能的启用
文章背景 有一个封装 RocketMq 的 client 的需求,用来提供给各项目收.发消息,但是项目当中常常只使用收或者发消息的单一功能,而且不同的项目 group 等并不相同而且不会变化,可以在项 ...
- Spring启动时获取自定义注解的属性值
1.自定义注解 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documen ...
- Spring Boot 中使用自定义注解,AOP 切面打印出入参日志及Dubbo链路追踪透传traceId
一.使用背景 开发排查系统问题用得最多的手段就是查看系统日志,在分布式环境中一般使用 ELK 来统一收集日志,但是在并发大时使用日志定位问题还是比较麻烦,由于大量的其他用户/其他线程的日志也一起输出穿 ...
- Spring 实现策略模式--自定义注解方式解耦if...else
策略模式 定义 定义一簇算法类,将每个算法分别封装起来,让他们可以互相替换,策略模式可以使算法的变化独立于使用它们的客户端 场景 使用策略模式,可以避免冗长的if-else 或 switch分支判断 ...
- Spring 捕捉校验参数异常并统一处理
使用 @Validated ,@Valid ,@NotBlank 之类的,请自行百度,本文着重与捕捉校验失败信息并封装返回出去 参考: https://mp.weixin.qq.com/s/EaZxY ...
- Spring MVC接受参数的注解
一.Request请求发出后,Headler Method是如何接收处理数据的? Headler Method绑定常用的参数注解,根据处理request的不同部分分为四类: A.处理 Request ...
- spring boot通过自定义注解和AOP拦截指定的请求
一 准备工作 1.1 添加依赖 通过spring boot创建好工程后,添加如下依赖,不然工程中无法使用切面的注解,就无法对制定的方法进行拦截 <dependency> <group ...
- Spring Boot中自定义注解+AOP实现主备库切换
摘要: 本篇文章的场景是做调度中心和监控中心时的需求,后端使用TDDL实现分表分库,需求:实现关键业务的查询监控,当用Mybatis查询数据时需要从主库切换到备库或者直接连到备库上查询,从而减小主库的 ...
随机推荐
- scrapy之360图片爬取
#今日目标 **scrapy之360图片爬取** 今天要爬取的是360美女图片,首先分析页面得知网页是动态加载,故需要先找到网页链接规律, 然后调用ImagesPipeline类实现图片爬取 *代码实 ...
- 09、RNA降解图的计算过程
RNA降解是影响芯片质量的一个很重要的因素,因为RNA是从5’开始降解的,所以理论5’的荧光强度要低于3’.RNA降解曲线可以表现这种趋势. 以样品GSM286756.CEL和GSM286757.CE ...
- java 字符串锁
package com.example.demo.controller; public class StringLock { public void method(String p) { // new ...
- 11.jQuery之自定义动画
注意:animate里面是一个对象,他有几个参数,详情可以参考官网 <style> div { position: absolute; width: 200px; height: 200p ...
- [转载]Jupyter Notebook中自动补全代码
原文地址:https://yq.aliyun.com/articles/667928 在公众号之前的文章中,已经介绍了在Jupyter Notebook中设置主题以及输出代码文件到pdf文件中,本文来 ...
- Git忽略文件的三个办法
方法一(并不好用) 在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件).这个文件每一行保存了一个匹配的规 ...
- Java动态加载类
详见:https://blog.csdn.net/zai_xia/article/details/80026325 扩展:java反射机制与动态加载类 https://www.cnblogs.com/ ...
- ConstantUtils
public class ConstantUtils { public static final Integer PAGE_SIZE=2; public static final Integer NA ...
- Codeforces 976 正方格蛇形走位 二维偏序包含区间 度数图构造 贪心心火牧最大dmg
A #include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; int main() { i ...
- docker概述和安装
一:概述 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用 ...