目录
@RequestMapping 请求映射
1、@RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上,用于类上时,表示类中的所有响应请求的方法都是以该地址作为父路径。
2、@RequestMapping 注解有如下属性
属性 | 描述 | 举例 |
---|---|---|
String name() default "" | ||
String[] value() default {} | 指定请求的实际地址,value值前缀"/"可写可不写 |
@RequestMapping("/visits"), @RequestMapping(value = {"visits1","visits2"}) |
String[] path() default {} | ||
RequestMethod[] method() default {} | 指定请求的 method 类型,有 GET、POST、PUT、DELETE 等 | @RequestMapping(value = "visits",method = RequestMethod.GET) |
String[] params() default {} | 指定 request 中必须包含某些参数值时才让该方法处理 | @RequestMapping(value = "test4", params = "token", method = RequestMethod.GET) |
String[] headers() default {} | 指定 request中必须包含某些指定的 header 值,才能让该方法处理请求 | |
String[] consumes() default {} | 指定处理请求的提交内容类型(Content-Type),例如 application/json, text/html | |
String[] produces() default {} | 指定返回的内容类型,仅当 request 请求头中的(Accept)类型中包含该指定类型才返回 |
3、举例如下:
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping(value = "visits")
public class VisitsController {
//http://localhost:8080/visits/test1
@RequestMapping(value = "test1", method = RequestMethod.GET)
public String visitsTest1(HttpServletRequest request) {
return request.getRequestURL().toString();
}
//http://localhost:8080/visits/test2, http://localhost:8080/visits/test3 都会进入
@RequestMapping(value = {"test2", "test3"}, method = RequestMethod.GET)
public String visitsTest23(HttpServletRequest request) {
return request.getRequestURL().toString();
}
//http://localhost:8080/visits/test4?token=98, 请求必须携带参数 token,否则匹配不上,进入不了方法
@RequestMapping(value = "test4", params = "token", method = RequestMethod.GET)
public String visitsTest4(HttpServletRequest request) {
return request.getRequestURL().toString();
}
//http://localhost:8080/visits/test5/his, http://localhost:8080/visits/test5/5x,路径结尾必须有路径变量
@RequestMapping(value = "test5/{type}", method = RequestMethod.POST)
public String visitsTest5(@PathVariable(value = "type") String type, HttpServletRequest request) {
return request.getRequestURL().toString() + "\t" + type;
}
}
4、随着现在 rest 风格的流行,人们更倾向与使用简洁的 @GetMapping、@PostMapping、@DeleteMapping、@PutMapping 等注解,它的属性与 @RequestMapping 完全一样。
@GetMapping(value = "test1") 等价于 @RequestMapping(value = "test1", method = RequestMethod.GET)
@PostMapping(value = "test5/{type}") 等价于 @RequestMapping(value = "test5/{type}", method = RequestMethod.POST)
@DeleteMapping 等价于 @RequestMapping(value = "test1", method = RequestMethod.DELETE)
@PutMapping 等价于 @RequestMapping(value = "test1", method = RequestMethod.PUT)
@SuppressWarnings 抑制警告
1、@SuppressWarnings 用于抑制编译器产生警告信息。
2、@SuppressWarnings 注解目标为类、成员变量、函数、函数参数、构造函数和函数的局部变量,不过建议注解应声明在最接近警告发生的位置。
@java.lang.SuppressWarnings("unused")
public void show1() {
int a;
}
public void show2() {
@SuppressWarnings("unused")
int a;
}
@SuppressWarnings(value = {"unused","unchecked"})
public void show3() {
int a;
}
3、@SuppressWarnings 抑制的警告类型如下:
警告类型 | 描述 |
---|---|
all | 抑制所有警告 |
boxing | 禁止相对于装箱/取消装箱操作的警告 |
cast | 抑制与强制转换操作相关的警告 |
dep-ann | 禁止显示与已弃用的批注相关的警告 |
deprecation | 取消与否决相关的警告 |
fallthrough | 禁止显示与switch语句中缺少中断相关的警告 |
finally | 抑制与finally块相关的不返回的警告 |
hiding | 抑制相对于隐藏变量的局部变量的警告 |
incomplete-switch | 禁止显示与switch语句中缺少项有关的警告(枚举情况) |
nls | 禁止显示与非nls字符串文本相关的警告 |
null | 抑制与空分析相关的警告 |
rawtypes | 对类参数使用泛型时,禁止显示与非特定类型相关的警告 |
restriction | 抑制与使用不鼓励或禁止的引用相关的警告 |
serial | 取消显示与可序列化类缺少串行版本uuid字段相关的警告 |
static-access | 禁止与不正确的静态访问相关的警告 |
synthetic-access | 禁止与来自内部类的未优化访问相关的警告 |
unchecked | 取消显示与未检查的操作相关的警告 |
unqualified-field-access | 取消与字段访问不合格相关的警告 |
unused | 禁止显示与未使用的代码相关的警告 |
@Scheduled 执行定时任务
1、@Scheduled 用在方法上,表示定时执行此方法,前提是这个类的实例需要由 Spring 容器管理,所以通常和 @Controller、@Service、@Repository、@Component 等注解一起使用。
2、如果是以前配置文件的方式,则需要在 spring 配置文件中配置扫描,而使用注解则更加简洁了。
1) Spring Boot 启动类上加上 @org.springframework.scheduling.annotation.EnableScheduling 注解,表示开启 @Scheduled
2)在 @Controller、@Service、@Repository、@Component 等组件中提供 @Scheduled 方法,表示定时执行此方法
3)默认情况下一个组件中的所有 @Scheduled 采用一个单线程执行,即一个 @Scheduled 执行完成后,另一个 @Scheduled 才能执行
4)如果想要并发执行组件中的所有 @Scheduled,则需要在类上加上 @org.springframework.scheduling.annotation.EnableAsync 注解,在方法上再加上 @org.springframework.scheduling.annotation.Async 注解
3、环境:Java JDK 1.8,Spring Boot 2.0.3,举例如下:
3.1 启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling //开启 @Scheduled 定时任务
public class WebAppApplication {
public static void main(String[] args) {
SpringApplication.run(WebAppApplication.class, args);
}
}
3.2 提供一个组件设置定时任务:
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
@EnableAsync //开启并发执行所有定时任务
public class SystemTimer {
//每隔3秒执行一次。上一次任务执行完成如果没有超时,则会继续延迟,如果已经超时,下一次任务则会立即执行
@Scheduled(cron = "0/3 * * * * ?")
@Async //并发执行此任务
public void time1() throws InterruptedException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println("time1->" + Thread.currentThread().getName() + " -> " + dateFormat.format(date));
Thread.sleep(5000);
}
//每隔3秒执行一次。下一次会在上一次任务执行完成后,继续延迟 3 秒后再执行。
@Scheduled(fixedDelay = 3000)
@Async //并发执行此任务
public void time2() throws InterruptedException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println("time2->" + Thread.currentThread().getName() + " -> " + dateFormat.format(date));
Thread.sleep(5000);
}
//每隔3秒执行一次。下一次会在上一次任务执行完成后执行,如果上一次任务执行超时,则下一次会立即执行
@Scheduled(fixedRate = 3000)
@Async //并发执行此任务
public void time3() throws InterruptedException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println("time3->" + Thread.currentThread().getName() + " -> " + dateFormat.format(date));
Thread.sleep(5000);
}
}
cron 表达式
1、Cron 表达式是一个字符串,用空格隔开,分为6或7个域,从左到右为:秒 分 小时 月份中的日期 月份 星期中的日期 年份。年份可写可不写。
@Scheduled(cron = "0/3 * * * * ?") : 每 3 秒钟执行一次
@Scheduled(cron = "0 0/30 * * * ?") :每 30 分钟在第 0 秒时刻执行一次
@Scheduled(cron = "0 0/30 0 * * * ?"):在每天 0 点内,每 30 分钟在第 0 秒时刻执行一次
@Scheduled(cron = "40 0/30 * * * ?") :每 30 分钟在第 40 秒时刻执行一次
@Scheduled(cron = "0 0 12 * * 2,6") :每周1、周5在 12:0:0 时刻执行一次
2、介绍 Cron 表达式的文章网上太多了,不再累述:
https://www.cnblogs.com/javahr/p/8318728.html https://www.jb51.net/article/138900.htm https://blog.csdn.net/qq_38505862/article/details/89153911 https://www.cnblogs.com/imstrive/p/9985120.html