SpringBoot接收前端參數的三種方法


都是以前的筆記了,有時間就整理出來了,SpringBoot接收前端參數的三種方法,首先第一種代碼:

@RestController
public class ControllerTest {
 //訪問路徑:http://localhost:8080/hello/5
    @GetMapping(value = "/hello/{id}")
    public String hello(@PathVariable("id") Integer id){
        return "ID:" + id;
    }
}

第二種:

  可以通過注解@RequestParam("xxx")來綁定指定的參數,在這里聲明的參數為ids,但是@RequestParam()注解中確實id,這個時候前端傳的值就應該為id,如果前台傳送的是ids的話,在這里是接收不到的。

@RestController
public class ControllerTest {
 //訪問路徑:http://localhost:8080/hello?id=1551
    @GetMapping(value = "/hello")
    public String hello(@RequestParam("id") Integer ids){
        return "ID:" + id;
    }
}

第三種像這種,如果傳進來的參數為空的話,可以設置為默認的:

    //訪問路徑:http://localhost:8080/hello?id=1551
    @GetMapping(value = "/hello")   
    public String hello(@RequestParam(value = "id", required = false, defaultValue = "5") Integer id){       
           return "ID:" + id;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM