基于注解的Controller接口這些高級功能,你都知道嗎?
環(huán)境:SpringBoot2.7.18
1. 簡介
基于注解的Controller接口,你可以使用 @RequestMapping 注解將請求映射到控制器方法。它有多種屬性,可根據(jù) URL、HTTP 方法、請求參數(shù)、頭和媒體類型進行匹配。該注解可以在類上使用它來表達共享映射,也可以在方法級使用它來縮小特定接口映射的范圍。
@RequestMapping 還有 HTTP 方法特定的快捷方式變體:
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
- @PatchMapping
通過上面的注解,我們能更加具體的去匹配我們的Controller接口方法。這些注解在開發(fā)中都是些基本的操作,并且在絕大多數(shù)情況下使用這些接口完全夠用了。接下來將介紹幾個高級用法。
2. 實戰(zhàn)案例
2.1 HEAD、OPTIONS請求
對于head,options請求Spring并沒有提供對應(yīng)的快捷注解,我們只能通過@RequestMapping注解來指定method屬性。
@GetMapping (和 @RequestMapping(method=HttpMethod.GET))支持 HTTP HEAD 透明請求映射??刂破鞣椒o需更改。在 javax.servlet.http.HttpServlet 中應(yīng)用的響應(yīng)封裝器可確保 Content-Length 標(biāo)頭被設(shè)置為寫入的字節(jié)數(shù)(而不會實際寫入響應(yīng))。如下示例:
@GetMapping("/h")
public Object h() {
System.out.println("header....") ;
return "h method..." ;
}
當(dāng)上面的接口以head方式請求時,也是可以成功進入該方法的只是并不會輸出內(nèi)容到客戶端,而僅僅是通過Content-Leng告知有多少個字節(jié)內(nèi)容。
圖片
響應(yīng)header中有Content-Length
圖片
對應(yīng)HEAD請求我們完全可以通過GET方式來解決。
默認情況下,HTTP OPTIONS 的處理方式是將 Allow 響應(yīng)頭設(shè)置為具有匹配 URL 模式的所有 @RequestMapping 方法中列出的 HTTP 方法列表。簡單說當(dāng)你通過OPTIONS請求一個URL時,Allow的響應(yīng)頭會設(shè)置為你@RequestMapping(method = ...)這里你method設(shè)置的列表+OPTIONS,如下示例:
@RequestMapping(value = "/o", method = {RequestMethod.DELETE, RequestMethod.HEAD})
public Object o() {
System.out.println("o....") ;
return "h method..." ;
}
當(dāng)上面的接口通過OPTIONS請求時,那么響應(yīng)header Allow將設(shè)置為如下:
圖片
如果你的@RequestMapping沒有指定method屬性值,那么Allow 頭信息會被設(shè)置為 GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS。上面的即可刪除method屬性再次請求如下:
圖片
2.2 自定義請求注解
在某些場景下你可能需要自定義自己的一些注解來配置通用的匹配模式,那么這時候通過自定義請求注解的方式是非常合適的,而不用你對每一個請求都去做重復(fù)的事。
自定義請求注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping
public @interface PackMapping {
@AliasFor(annotation = RequestMapping.class)
String name() default "";
@AliasFor(annotation = RequestMapping.class)
String[] value() default {};
@AliasFor(annotation = RequestMapping.class)
// 配置只能是GET或POST方法
RequestMethod[] method() default {RequestMethod.GET, RequestMethod.POST} ;
@AliasFor(annotation = RequestMapping.class)
String[] path() default {};
@AliasFor(annotation = RequestMapping.class)
String[] params() default {};
// 設(shè)置請求header必須包含X-API-Version并且值必須是v1
@AliasFor(annotation = RequestMapping.class)
String[] headers() default {"X-API-Versinotallow=v1"};
@AliasFor(annotation = RequestMapping.class)
String[] consumes() default {};
@AliasFor(annotation = RequestMapping.class)
String[] produces() default {};
}
測試Controller接口
@PackMapping("/v")
public Object v() {
System.out.println("自定義請求注解....") ;
return "PackMapping method..." ;
}
輸出結(jié)果
圖片
請求中必須攜帶X-API-Version請求header。
2.3 動態(tài)顯示注冊接口
你可以通過編程方式注冊處理程序方法,這些方法可以用于動態(tài)注冊或高級情況,例如不同URL下的同一處理程序的不同實例,如下示例:
@Configuration
public class CustomWebConfig {
@Autowired
public void setHandlerMapping(RequestMappingHandlerMapping mapping, UserService handler)
throws NoSuchMethodException {
BuilderConfiguration options = new BuilderConfiguration() ;
options.setPatternParser(new PathPatternParser()) ;
RequestMappingInfo info = RequestMappingInfo.paths("/api/user/{id}").methods(RequestMethod.GET).options(options ).build();
Method method = UserHandler.class.getMethod("getUser", Integer.class) ;
mapping.registerMapping(info, handler, method);
}
}
UserService
@Component
public class UserHandler {
@ResponseBody
public User getUser(@PathVairable("id")Integer id) {
User user = new User() ;
user.setId(id) ;
user.setName("張三") ;
return user ;
}
}
輸出結(jié)果
圖片
以上是本篇文章的全部內(nèi)容,如對你有幫助就請作者吃個棒棒糖??。