1. 程式人生 > >SpringBoot 入門筆記

SpringBoot 入門筆記

ring 返回 一個 map 組合 pin ble ppi eth

1. Spring 4.3中引入了:

  @GetMapping

  @PostMapping

  @PutMapping

  @DeleteMapping

  @PatchMapping

2. @RequestMapping如果沒有指定請求方法,將接收GET、PUT、POST等所有請求

  @GetMapping是一個組合註解,是@RequstMapping(method=RequestMethod.GET)的縮寫。

  @PostMapping是一個組合註解,是@RequestMapping(method=RequestMethod.POST)的縮寫。

3. @RequestMapping會返回一個 ModelAndView對象,也就是說:可以返回一個Model對象,也可以返回一個View對象。

  示例代碼,返回Model對象:

    @GetMapping("/users")
    public List<Users> getAllUsers(){
        return userRepository.findAll();
    }

  示例代碼,返回View對象:

@RequestMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name, Model model) {
        model.addAttribute("name", name);
        
return "hello"  // 返回值"hello"並非直接將字符串返回給瀏覽器,而是尋找名字為hello的模板進行渲染 }

   

SpringBoot 入門筆記