SpringBoot集成SpringMVC

SpringBoot集成SpringMVC

    • 1. SpringMVC常见注解
      • 1. 1 @RestController
      • 1. 2 @GetMapping
      • 1. 3 @PostMapping
      • 1. 4 @DeleteMapping
      • 1. 5 @PutMapping
    • 2. SpringBoot实现RESTful
      • 2.1 认识RESTful
      • 2.2 SpringBoot开发RESTful主要注解
        • 2.2.1 @PathVariable
        • 2.2.2 @PostMapping
      • 3.测试

1. SpringMVC常见注解

@RequestMapping的请求方式

  1. 如果方法上的@RequestMapping注解没有设置method属性,则请求方式支持:GET和POST请求
  2. 如果方法上的@RequestMapping注解设置了method属性,则只能是相应的请求方式可以访问。

@Controller
@ResponseBody
@RestController
@RequestMapping
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping

1. 1 @RestController

@RestController = @Controller+@ResponseBody
所有方法都是JSON对象,控制层的注解可以换成@RestController
相当于控制层类上加上@Controller + 方法上加@ResponseBody
意味着当前控制层类中所有方法返回值都是JSON对象

1. 2 @GetMapping

@GetMapping(value=“/query”) = @RequestMapping(value=“/query”,method = RequestMethod.GET)
只接收GET请求,请求方式不对就报405错误
该注解通常在查询数据的时候使用 ----->查询

1. 3 @PostMapping

@PostMapping(value=“/insert”) = @RequestMapping(value=“/insert”,method = RequestMethod.POST)
该注解通常在新增数据的时候使用 ----->新增

1. 4 @DeleteMapping

@DeleteMapping(value=“/delete”) = @RequestMapping(value=“/delete”,method = RequestMethod.DELETE)
该注解通常在删除数据的时候使用 ----->删除

1. 5 @PutMapping

@PutMapping(value=“/update”) = @RequestMapping(value=“/update”,method = RequestMethod.PUT)
该注解通常在修改数据的时候使用 ----->更新

2. SpringBoot实现RESTful

2.1 认识RESTful

RESTful:是一种互联网软件架构设计的风格
它只是提出了一组客户端和服务端交互时的架构理念和设计原则,基于这种理念和原则设计的接口可以更简洁,更有层次。

任何的技术都可以实现这种理念,如果一个架构符合REST原则,就称它为RESTful架构。

比如:
我们要访问一个http接口:http://localhost:8080/boot/order?id=1001&status=1
采用RESTful风格则http地址为:http://localhost:8080/boot/order/1001/1

2.2 SpringBoot开发RESTful主要注解

2.2.1 @PathVariable

@PathVariable
获取url中的数据
该注解是实现RESTful最主要的一个注解

2.2.2 @PostMapping

@PostMapping
接收和处理Post方式的请求

 @PostMapping("/student/{id}/{name}")   //RESTful风格
    public Object student(@PathVariable("id") Integer id,
                          @PathVariable("name") String name){
        HashMap<String,Object> restMap = new HashMap<>();
        restMap.put("id",id);
        restMap.put("name",name);
        return restMap;
    }

注意:请求路径冲突问题

1.通常在RESTful风格方法的请求方式会按增删改查的请求方式来区分
2.修改请求路径
3.RESTful请求风格要求路径中使用的单词都是名词,最好不要出现动词
增POST请求
删DELETE请求
改PUT请求
查GET请求

如果传的参数不是数据库表中的字段可以不采用斜杠

3.测试

请求地址相同,通过请求方式不同来区分请求

@RestController
public class RestfulController {
    @GetMapping("/rest")
    public Object rest(Integer id ,String name){
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        return student;
    }
    @GetMapping("/student/{id}/{name}")   //RESTful风格
    public Object student(@PathVariable("id") Integer id,
                          @PathVariable("name") String name){
        HashMap<String,Object> restMap = new HashMap<>();
        restMap.put("id",id);
        restMap.put("name",name);
        return restMap;
    }
    @PostMapping("/student/{id}/{age}")
    public Object student2(@PathVariable("id") Integer id,
                          @PathVariable("age") Integer age){
        HashMap<String,Object> restMap = new HashMap<>();
        restMap.put("id",id);
        restMap.put("age",age);
        return restMap;
    }
}

GET方式请求
SpringBoot集成SpringMVC_第1张图片POST方式请求
SpringBoot集成SpringMVC_第2张图片

你可能感兴趣的:(Spring,Boot,spring,boot,java,restful)