1. 程式人生 > >SpringBoot2.X學習總結:(1)註解部分

SpringBoot2.X學習總結:(1)註解部分

  1. @SpringBootApplication包含了@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan,一個註解相當於三個註解
  2. @RestController 相當於 @[email protected]
  3. GET,POST,PUT等請求相關

         1)單一引數

@RequestMapping(path = "/{id}",method = RequestMethod.GET)
public String getUser(@PathVariable String id){
}
@GetMapping("/api/get")
public String getUser(String id){
}

     2)多個引數

@RequestMapping(path = "/{depid}/{userid}",method=RequestMethod.GET)
public String 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)
...

       4)@RequestParam(value = "name",required = true),可以設定預設值

       5)@RequestMapping + @RequestBody請求體對映實體類

             注意:需要指定http頭:content-type為application/json

       6)@RequestHeader 請求頭,比如鑑權

               @RequestHeader("access_token") String accessToken

       7)HttpServletRequest request自動注入獲取引數