目錄
@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