欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot如何獲取Get請求參數(shù)詳解

 更新時間:2022年12月06日 10:30:29   作者:瞎胡扯  
SpringBoot為我們封裝了許多簡便的獲取請求參數(shù)的方法,下面這篇文章主要給大家介紹了關于SpringBoot如何獲取Get請求參數(shù)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

前言

利用 Spring Boot 來制作 Web 應用,就必定會涉及到前端與后臺之間互相傳遞參數(shù)。下面演示 Controller 如何接收以 GET 方式傳遞過來的參數(shù)。

一、直接在請求路徑中

(1)、假設請求地址是如下這種 RESTful 風格,Springboot 這個參數(shù)值直接放在路徑里面

http://localhost:8080/hello/Springboot

(2)、Controller 定義,如下:

@RestController
public class HelloController {
    @GetMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name){
 
        return "你好," + name + " !";
    }
}

二、參數(shù)跟在 ? 號后面

1,獲取參數(shù)的基本方法

(1)假設請求地址是如下這種傳統(tǒng)方式,參數(shù)跟在問號后面:

http://localhost:8080/hello?name=Springboot

(2)Controller 定義如下所示:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(@RequestParam("name") String name){
 
        return "你好," + name + " !";
    }
}

可以把在注解 @RequestParam 添加  required = false 設置參數(shù)為非必輸項
可以把在注解 @RequestParam 添加  defaultValue 參數(shù) 設置默認值。

2、使用 map 來接收參數(shù)

(1)Controller 還可以直接使用 map 來接收所有的請求參數(shù):

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(@RequestParam Map<String, Object> map){
 
        return "圖書: " + map.get("name") +  " 的作者為: " + map.get("author");
    }
}

(2)、結果如下所示:

3、接收一個集合

(1)、Controller 定義接收 數(shù)組或者List的方法,如下:

@RestController
public class HelloController {
 
    @GetMapping("/array")
    public String array(@RequestParam("name") String[] names){
 
        System.out.println("我是數(shù)組接收的參數(shù):" + Arrays.toString(names));
 
        return "我是數(shù)組接收的參數(shù):" + Arrays.toString(names);
    }
 
    @GetMapping("/list")
    public String list(@RequestParam("name")List<String> names){
        System.out.println("我是List接收的參數(shù):" + names);
        return "我是List接收的參數(shù):" + names.toString();
    }
 
}

(2)、測試結果

4、通過對象接收參數(shù)

如果一個 get 請求的參數(shù)太多,我們構造一個對象來簡化參數(shù)的接收方式。

(1)、定義一個基本對象Book 和 Controller

public class Book {
    private String name;
    private String author;
    private Double price;
   // 省略 getter setter 方法
}
@RestController
public class BookController {
    @GetMapping("/book")
    public Book book(Book book){
        System.out.println("接收到的參數(shù)為:" + book);
        return  book;
    }
}

 (2)、基本用法:

(3)、構造多個對象來接收參數(shù),代碼如下所示:

public class User {
    private String username;
    private String password;
    // 省略 getter setter 方法
}
@RestController
public class BookController {
    @GetMapping("/book")
    public Book book(Book book, User user){
        System.out.println("接收到的參數(shù)為:" + book);
        System.out.println("接收到的參數(shù)為:" + user);
        return  book;
    }
}

訪問:

輸出結果:

(4)、指定前綴。

如果在多個對象參數(shù)的方法中,對象之間存在相同的 屬性時 可以結合  @InitBinder 指定前綴的方式處理。如下所示,Book 對象和 Author 對象中都存在 name 屬性。

    除了在 Controller 里單獨定義預處理方法外,我們還可以通過 @ControllerAdvice 結合 @InitBinder 來定義全局的參數(shù)預處理方法,方便各個 Controller 使用

public class Book {
    private String name;
    private String author;
}
 
 
public class Author {
    private String name;
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    @Override
    public String toString() {
        return "Author{" +
                "name='" + name + '\'' +
                '}';
    }
}
@RestController
public class BookController {
    @GetMapping("/book")
    public Book book(@ModelAttribute("b") Book book, @ModelAttribute("a") Author author){
        System.out.println("接收到的參數(shù)為:" + book);
        System.out.println("接收到的參數(shù)為:" + author);
        return  book;
    }
 
    // 添加  InitBinder 指定 前綴 b
    @InitBinder("b")
    private void initBinderB(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("b.");
    }
 
    // 添加  InitBinder 指定 前綴 a
    @InitBinder("a")
    private void initBinderA(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("a.");
    }
}

訪問:

結果:

總結

到此這篇關于SpringBoot如何獲取Get請求參數(shù)的文章就介紹到這了,更多相關SpringBoot獲取Get請求參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論