天天看點

SpringMVC(SpringBoot)--請求映射--方式/比對/占位符(将url作為參數/@PathVariable)等

簡介

        SpringMVC/SpringBoot中,我們是通過@RequestMapping來指定請求映射的,本文介紹如下内容

  1. @RequestMapping的使用方法
  2. 映射單個URL/多個URL的方法
  3. 斜杠/對路徑的影響
  4. 五種映射方式
  5. 請求方法的參數的依賴注入

@RequestMapping簡介

@RequestMapping(value="/testRequestMapping", 
                method={RequestMethod.POST},
                params = {"username=jack"}, 
                headers = {"Accept"})      

value/path:指定請求路徑,當有注解後面隻有一個參數的時候可以直接寫路徑

params:指定請求參數中必須包含的屬性。本處是:必須包含username屬性,且值必須為jack

headers:指定請求的資訊中必須包含的請求頭資訊。

RequestMapping注解可以用在類上或者方法上。

url映射配置方法

​映射單個URL​

@RequestMapping("") 或 @RequestMapping(value="")

​映射多個URL​

@RequestMapping({"xxx","yyy"}) 或 @RequestMapping(value={"xxx","yyy"})

​斜杠/​

路徑開頭是否加斜杠/均可,建議加上,如:@RequestMapping("/hello")

@RequestMapping("")與@RequestMapping("/")效果一樣,都是根路徑。

五種映射方式

@RequestMapping 一共有五種映射方式

​映射方式​ ​說明​ ​示例​
标準映射 标準URL 映射是最簡單的一種映射

@RequestMapping("/hello")

@RequestMapping({"/hello","/world/test"})

Ant風格

Ant 通配符有三種:

?:比對任何單字元

*:比對任意數量的字元(含 0 個)

**:比對任意數量的目錄(含 0 個)

(1)@RequestMapping("/?/hello/")

(2)@RequestMapping("/*/hello")

(3)@RequestMapping("/**/hello")

占位符

可以通過一個或多個 {} 占位符映射。

若 URL 中的 userId 是純數字,那麼使用 @PathVariable 做綁定時,可以根據自己的需求将方法參數類型設定為 Long、 Integer、String等。

@PathVariable("") 不能簡寫為 @PathVariable

@RequestMapping("/user/{userId}/show")

public User show(@PathVariable("userId") Long userId) {

    User user = userService.getById(userId);

    return user;

}

限制請求方法 HTTP 請求中最常用的請求方法是 GET、POST,還有其他的一些方法,如:DELET、PUT、HEAD 等

@RequestMapping(value="/hello", method=RequestMethod.POST)

如需限制多個請求方法,以大括号包圍,逗号隔開即可,例如:

method={RequestMethod.GET,RequestMethod.POST}

限制請求參數

請求中必須帶有userId 參數

參數的限制規則如下:

(1)params="userId" 請求參數中必須包含 userId

(2)params="!userId" 請求參數中不能包含 userId

(3)params="userId!=1" 請求參數中必須包含 userId,但不能為 1

(4)params={"userId","userName"} 必須包含 userId 和 userName 參數

(5)params={"userId=1","userName!=2"}必須包含 userId 和 userName 參數,且userId值必須為1,userName值必須不為2。

@RequestMapping(value="/user/show",params="userId")

public User show(@RequestParam("userId") Long userId) {

    User user = userService.getById(userId);

    return user;

}

參數的依賴注入

@RequestMapping注解的方法的參數會自動注入,示例如下。

<a href="param/testParam?username=hehe&password=123"></a>

public class User {
    private String name;
    private String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}      
//請求參數綁定
   @RequestMapping("/testParam")
   public String testParam(String username,String password, User user){
       System.out.println("執行了...");
       System.out.println("使用者名:"+username);
       System.out.println("密碼:"+password);
       User.name = username;
       User.password = password;
       System.out.println("User");
       System.out.println("  " + User.name);
       System.out.println("  " + User.password);
       return "success";
   }      

​運作結果   ​

執行了...

使用者名:hehe

密碼:123

User

  hehe

  123

其他網址