当前位置:网站首页>自定义注解实现验证信息的功能
自定义注解实现验证信息的功能
2022-07-01 13:29:00 【喜欢历史的工科生】
自定义注解实现验证逻辑
- 导包
<!-- hibernate 验证框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
- 定义BO对象,前端传来的业务对象,对象中的属性需要校验
package com.imooc.boapi;
//import com.imooc.annotationcustom.VlogId;
import com.imooc.annotationcustom.VlogId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class VlogBOApi {
private String id;
@VlogId(message = "用户不存在,禁止插入视频")
private String vlogerId;
private String url;
private String cover;
private String title;
private Integer width;
private Integer height;
private Integer likeCounts;
private Integer commentsCounts;
}
- 定义一个校验注解
package com.imooc.annotationcustom;
import javax.validation.Constraint;
import javax.validation.Payload;
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)
@Constraint(validatedBy = VlogIdValidator.class) //校验的逻辑处理类
public @interface VlogId {
String message();
/** * 将validator进行分类,不同的类group中会执行不同的validator操作 * * @return validator的分类类型 */
Class<?>[] groups() default {
};
/** * 主要是针对bean,很少使用 * * @return 负载 */
Class<? extends Payload>[] payload() default {
};
}
- 同时需要一个校验类,实现一个注解检验的逻辑
package com.imooc.annotationcustom;
import com.imooc.mapper.UsersMapper;
import com.imooc.pojo.Users;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
@Slf4j
@Component
public class VlogIdValidator implements ConstraintValidator<VlogId, String> {
@Autowired
UsersMapper usersMapper;
@Override
public void initialize(VlogId constraintAnnotation) {
log.info("注解逻辑初始化");
}
@Override
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
Users user = new Users();
user.setId(s);
Users reslut = usersMapper.selectByPrimaryKey(user);
System.out.println("注解拦截生效+package com.imooc.annotationcustom;");
if (reslut == null) {
return false;
} else {
return true;
}
}
}
- 业务代码
@PostMapping("publish")
public GraceJSONResult publish(@Valid @RequestBody VlogBOApi vlogBO) {
// vlogService.createVlog(vlogBO);
return GraceJSONResult.ok();
}
- 校验失败异常,定义一个全局异常捕获,将这个错误返回给前端
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public GraceJSONResult returnMethodArgumentNotValid(MethodArgumentNotValidException e) {
BindingResult result = e.getBindingResult();
Map<String, String> map = getErrors(result);
return GraceJSONResult.errorMap(map);
}
自定义注解来简化开发
目的:做一个点赞次数拦截的功能。过程:需要拦截以后自行确定多少时间内的点赞次数
- 定义注解:
package com.imooc.annotationcustom;
import java.lang.annotation.*;
@Inherited
@Documented
@Target({
ElementType.FIELD, ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestLimit {
/** * 时间内 秒为单位 * @return */
int second() default 10;
/*** * 允许访问次数 * @return */
int maxCount() default 5;
}
- 拦截器代码,这里需要获取接口上的自定义注解handlerMethod.getMethodAnnotation
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
//获取RequestLimit注解
RequestLimit requestLimit = handlerMethod.getMethodAnnotation(RequestLimit.class);
if (null == requestLimit) {
System.out.println("没有获取到注解");
return true; //每加注解说明没有限制,多少次都ok
}
//限制的时间范围
int seconds = requestLimit.second();
//限制时间内的最大次数
int maxCount = requestLimit.maxCount();
//获取用户ip
String userIp = IPUtil.getRequestIp(request);
//判断redis中是否有ip
boolean keyIsExist = redis.keyIsExist(LIKE_VLOG_OPERATE_COUNT + ":" + userIp);
//如果没有这个键,说明当前用户在20s内没有点过赞,因为这个拦截器就是拦截点赞的后端接口,所以如果没有这个键就设置一个键
if (keyIsExist == false) {
redis.set(LIKE_VLOG_OPERATE_COUNT + ":" + userIp, "0", seconds);
System.out.println("ok");
return true;
}
// 返回true,就要判断这个键对应的值的个数
String res = redis.get(LIKE_VLOG_OPERATE_COUNT + ":" + userIp);
if (Integer.valueOf(res) >= maxCount) {
GraceException.display(ResponseStatusEnum.LIKE_VLOG_WAIT_ERROR);
return false;
}
}
return true;
}
- 业务代码
@PostMapping("like")
//TODO: 这里需要调试
@RequestLimit(maxCount = 3, second = 60)
public GraceJSONResult like(@RequestParam String userId,
@RequestParam String vlogerId,
@RequestParam String vlogId,
HttpServletRequest request) {
vlogService.userLikeVlog(userId, vlogId);
redis.increment(REDIS_VLOGER_BE_LIKED_COUNTS + ":" + vlogerId, 1);
redis.increment(REDIS_VLOG_BE_LIKED_COUNTS + ":" + vlogId, 1);
redis.set(REDIS_USER_LIKE_VLOG + ":" + userId + ":" + vlogId, "1");
//------------------------------------------//
//新增功能,限制点赞次数,
String userIp = IPUtil.getRequestIp(request);
redis.increment(LIKE_VLOG_OPERATE_COUNT + ":" + userIp,1);
//-------------------------------------------//
return GraceJSONResult.ok();
}
边栏推荐
- 单工,半双工,全双工区别以及TDD和FDD区别
- Report on the "14th five year plan" and scale prospect prediction of China's laser processing equipment manufacturing industry Ⓢ 2022 ~ 2028
- Use of shutter SQLite
- 运行游戏时出现0xc000007b错误的解决方法[通俗易懂]
- Cs5268 advantages replace ag9321mcq typec multi in one docking station scheme
- 受益互联网出海 汇量科技业绩重回高增长
- MySQL六十六问,两万字+五十图详解!复习必备
- Uni app realizes advertisement scroll bar
- 9. Use of better scroll and ref
- Qtdeisgner, pyuic detailed use tutorial interface and function logic separation (nanny teaching)
猜你喜欢

Learning to use livedata and ViewModel will make it easier for you to write business

Professor Li Zexiang, Hong Kong University of science and technology: I'm wrong. Why is engineering consciousness more important than the best university?

一款Flutter版的记事本

面试题目总结(1) https中间人攻击,ConcurrentHashMap的原理 ,serialVersionUID常量,redis单线程,

When you really learn databinding, you will find "this thing is really fragrant"!
![[安网杯 2021] REV WP](/img/98/ea5c241e2b8f3ae4c76e1c75c9e0d1.png)
[安网杯 2021] REV WP

学会使用LiveData和ViewModel,我相信会让你在写业务时变得轻松

Liu Dui (fire line safety) - risk discovery in cloudy environment

陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全

AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集
随机推荐
Nexus builds NPM dependent private database
Sign APK with command line
开源者的自我修养|为 ShardingSphere 贡献了千万行代码的程序员,后来当了 CEO
Have you ever encountered the problem that flynk monitors the PostgreSQL database and checkpoints cannot be used
受益互联网出海 汇量科技业绩重回高增长
Global and Chinese silicone defoamer production and marketing demand and investment forecast analysis report Ⓨ 2022 ~ 2027
Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security
龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
French Data Protection Agency: using Google Analytics or violating gdpr
小程序- view中多个text换行
流量管理技术
Sharing with the best paper winner of CV Summit: how is a good paper refined?
Explain IO multiplexing, select, poll, epoll in detail
北斗通信模块 北斗gps模块 北斗通信终端DTU
About fossage 2.0 "meta force meta universe system development logic scheme (details)
Computer network interview knowledge points
Global and Chinese polypropylene industry prospect analysis and market demand forecast report Ⓝ 2022 ~ 2027
Understand the window query function of tdengine in one article
6. Wiper part
小程序-小程序图表库(F2图表库)