小D課堂 - 零基礎入門SpringBoot2.X到實戰_第2節 SpringBoot接口Http協議開發實戰_八、SpringBoot基礎HTTP接口GET請求實戰


三、SpringBoot基礎HTTP接口GET請求實戰
    簡介:講解springboot接口,http的get請求,各個註解使用
        一、GET請求
            一、單一參數@RequestMapping(path = "/{id}", method = RequestMethod.GET)
                1) public String getUser(@PathVariable String id ) {}
                
                2)@RequestMapping(path = "/{depid}/{userid}", method = RequestMethod.GET) 能夠同時指定多個提交方法
                getUser(@PathVariable("depid") String departmentID,@PathVariable("userid") String userid)

                3)一個頂倆
                @GetMapping = @RequestMapping(method = RequestMethod.GET)
                @PostMapping = @RequestMapping(method = RequestMethod.POST)
                @PutMapping = @RequestMapping(method = RequestMethod.PUT)
                @DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)

                4)@RequestParam(value = "name", required = true)
                    能夠設置默認值,好比分頁 

                4)@RequestBody 請求體映射實體類
                    須要指定http頭爲 content-type爲application/json charset=utf-8

                5)@RequestHeader 請求頭,好比鑑權
                    @RequestHeader("access_token") String accessToken

                6)HttpServletRequest request自動注入獲取參數前端

開始

get請求註解比較多因此單獨來說
新建一個getController


使用註解RestController表示返回json給前端

接口統一用小寫字母

使用@PathVariable獲取的city_id賦值給userId

使用postman測試
spring

@GetMapping

簡化,直接定義方位get請求,使用@GetMapping。是Spring Boot給咱們提供的註解

點進去@GetMapping實際上就包裝了一層@RequestMapping裏面默認設置了method是get




故意用post方式請求
json

@RequestParam

參數的默認值。
name是別名。接口要傳遞的參數爲page=110
required表示字段是必須的



不傳page
springboot

@RequestBody





相應的數據
app

@RequestHeader




斷點調試





HttpServletRequest


 

相關文章
相關標籤/搜索