小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_8、SpringBoot基础HTTP接口GET请求实战

时间:2019-08-22
本文章向大家介绍小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_8、SpringBoot基础HTTP接口GET请求实战,主要包括小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_8、SpringBoot基础HTTP接口GET请求实战使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。


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




 

原文地址:https://www.cnblogs.com/wangjunwei/p/11393680.html