SpringBoot 入门笔记

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的模板进行渲染
    }

   

猜你喜欢

转载自www.cnblogs.com/vincenshen/p/10264406.html
今日推荐