zoukankan      html  css  js  c++  java
  • 小D课堂


    3、SpringBoot基础HTTP接口GET请求实战
        简介:讲解springboot接口,http的get请求,各个注解使用
            1、GET请求
                1、单一参数@RequestMapping(path = "/{id}", method = RequestMethod.GET)
                    1) public String getUser(@PathVariable String id ) {}
                    
                    2)@RequestMapping(path = "/{depid}/{userid}", method = RequestMethod.GET) 可以同时指定多个提交方法
                    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)
                    @DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)

                    4)@RequestParam(value = "name", required = true)
                        可以设置默认值,比如分页 

                    4)@RequestBody 请求体映射实体类
                        需要指定http头为 content-type为application/json charset=utf-8

                    5)@RequestHeader 请求头,比如鉴权
                        @RequestHeader("access_token") String accessToken

                    6)HttpServletRequest request自动注入获取参数

    开始

    get请求注解比较多所以单独来讲
    新建一个getController


    使用注解RestController表示返回json给前端

    接口统一用小写字母

    使用@PathVariable获取的city_id赋值给userId

    使用postman测试

    @GetMapping

    简化,直接定义方位get请求,使用@GetMapping。是Spring Boot给我们提供的注解

    点进去@GetMapping实际上就包装了一层@RequestMapping里面默认设置了method是get




    故意用post方式请求

    @RequestParam

    参数的默认值。
    name是别名。接口要传递的参数为page=110
    required表示字段是必须的



    不传page

    @RequestBody





    相应的数据

    @RequestHeader




    断点调试





    HttpServletRequest




     

  • 相关阅读:
    Redis Java客户端之Lettuce
    Redis Java客户端之Redisson
    Redis Java客户端的比较
    Redis布隆过滤器
    过期删除策略和内存淘汰策略
    集群模式详解
    哨兵模式详解
    Redis Java客户端之Jedis
    主从复制
    AOF持久化
  • 原文地址:https://www.cnblogs.com/wangjunwei/p/11393680.html
Copyright © 2011-2022 走看看