SpringBoot 4 和SpringMVC的整合

1.预热

0. RESTFul

  一种互联网软件架构设计的风格,但它并不是标准,它只是提出了一组客户端和服务器 交互时的架构理念和设计原则,基于这种理念和原则设计的接口可以更简洁,任何的技术都可以实现这种理念,如果一个架构符合 REST 原则,就称它为 RESTFul 架构

  RESTFul 请求风格要求路径中使用的单词都是名词,最好不要出现动词

   比如我们要访问一个 http 接口:http://localhost:8080/boot/order?id=1024&status=1

​ 采用 RESTFul 风格则 http 地址为:http://localhost:8080/boot/order/1024/1

穿衣服的风格

1.RestController

  • @RestController=@ResponseBody+@Controller

  • 假设controller方法里面的返回值全部是json 可以使用,不需要在每一方法添加@ResponseBody

  • 意味着当前控制层类中所有方法返回都是JSON对象

2.各种请求注解

那种请求都可以接盘

@RequestMapping(value="/get",method={RequestMethod.GET,RequestMethod.POST})
==@RequestMapping(value="/get")

eg.

@GetMapping(value="/s") ==
@RequestMapping(value="/s",method=RequestMethod.GET)

请求不对,会爆405错误

  • 根据不同的请求方式,注解也是不同

    • GetMapping 查询数据
    • PostMapper 新增数据
    • PutMapper 修改数据
    • DeleteMapper 删除数据
  • 发送post 请求体

  • 发送json数据

[]  json数组
{}  json 对象 
"key":"value"   key 和value 必须加""  数值型数据除外

3.@PathVariable

  获取 url 中的数据, 该注解是实现 RESTFul 最主要的一个注解

2.代码

/**
 * RESTFul风格的api
 */
@RestController
public class RESTFullController {

    @Autowired
    private StuInfoService StuInfoService;
    /**
     * RESTFul风格的查询
     *      将参数放到请求的地址中拼接
     *      GET:
     *          http://localhost:8080/123
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    //使用@PathVariable将路径中的变量参数,映射到方法的行参中
    //如果形参的属性名称和路径中的名称一致,就不用设置注解中的name或value属性
    //尽量在注解中填写name或value
    public Object findById(@PathVariable(name = "id") String id){
        return StuInfoService.findById(id);
    }

    
    @GetMapping("/{id}/{name}")
    public Object findByExample(@PathVariable(name = "id") String id,@PathVariable(name = "name")String name){
        return StuInfoService.findByIdAndName(id,name);
    }

    //Post:
    //http://localhost:8080
    @PostMapping
    //使用@PathVariable将路径中的变量参数,映射到方法的行参中
    //如果形参的属性名称和路径中的名称一致,就不用设置注解中的name或value属性
    //尽量在注解中填写name或value
    public Object insert(StuInfo StuInfo){
        return StuInfoService.insert(StuInfo);
    }

    @PutMapping
    public Object update(StuInfo StuInfo){
        return StuInfoService.updateSelective(StuInfo);
    }

    @DeleteMapping("/{id}")
    public Object deleteById(@PathVariable(name = "id") String id){
        return StuInfoService.deleteById(id);
    }

    //请求参数放请求路径
    @RequestMapping(value="/su/{id}/{email}")
    public StuInfo get(@PathVariable("id") Integer id,@PathVariable("email") String email) {
        StuInfo st=new StuInfo();
        st.setEmail(email);
        st.setId(id); //map:  new ---》put
        return st;
    }
    
    
}
  • 多个参数的需求实体类的实现
 @Override
    public Object findByIdAndName(String id, String name) {
        StuInfoExample example = new  StuInfoExample();

         StuInfoExample.Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(id);

        criteria.andNameEqualTo(name);

        return  StuInfoMapper.selectByExample(example);
    }

posted @ 2020-10-01 14:21  Tony小哥  阅读(122)  评论(0编辑  收藏  举报