本文包含了,生成cookie 和 带cookie以及 带参数和不带参数的的get请求。使用了Springboot 和SwaggerUi框架。
SwaggerUi是一个自动生成接口文档的插件,用了SwaggerUi就可以不用写接口文档了。
Swagger 接口文档地址:http://127.0.0.1:8080/swagger-ui.html
package com.course.server; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; import java.util.Objects; /** * 返回一个cookies的get请求接口 */ @RestController @Api(value = "/", description = "这是我全部的get方法") public class MyGetMethod { @RequestMapping(value = "/getCookies", method = RequestMethod.GET) @ApiOperation(value = "通过这个方法可以获取到cookies", httpMethod = "Get") public String getCookies(HttpServletResponse response) { //HttpServletRequest 装请求信息的类 //HttpServletResponse 装响应信息的类 是一个类,不是参数 Cookie cookie = new Cookie("login", "true"); response.addCookie(cookie); return "恭喜你获得cookies信息成功!"; } /** * 要求客户端带cookies访问 */ @RequestMapping(value = "get/with/cookies", method = RequestMethod.GET) @ApiOperation(value = "要求客户端带cookies访问", httpMethod = "Get") public String getWithCookies(HttpServletRequest request) { Cookie[] cookies = request.getCookies(); if (Objects.isNull(cookies)) { return "你必须携带一个cookie!"; } for (Cookie cookie : cookies) { if (cookie.getName().equals("login") && cookie.getValue().equals("true")) { return "恭喜你访问成功!"; } else { return "你的cookie值错误"; } } return "你必须携带一个cookie!"; } /** * 开发一个需要携带参数的get 请求 * 第一种实现方式 url: key=value&key=value * 模拟获取商品列表 */ @RequestMapping(value = "/get/with/param", method = RequestMethod.GET) @ApiOperation(value = "开发一个需要携带参数的get 请求, url: key=value&key=value", httpMethod = "GET") public Map<String, Integer> getList(@RequestParam Integer start, @RequestParam Integer end) { Map<String, Integer> mylist = new HashMap<>(); mylist.put("鞋", 400); mylist.put("衬衫", 300); mylist.put("裤子", 400); return mylist; } /** * 第二种需要携带参数访问的get请求 * url: ip:port/get/with/param/10/20 http://127.0.0.1:8080/get/with/param/10/20 */ @RequestMapping(value = "/get/with/param/{start}/{end}") @ApiOperation(value = "开发一个需要携带参数的get 请求, url: ip:port/get/with/param/10/20", httpMethod = "GET") public Map myGetList(@PathVariable Integer start, @PathVariable Integer end) { Map<String, Integer> mylist = new HashMap<>(); mylist.put("鞋", 400); mylist.put("衬衫", 300); mylist.put("裤子", 400); return mylist; } }
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan("com.course") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
package com.course.config; import org.mapstruct.BeanMapping; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration //加载配置文件 @EnableSwagger2 //加载配置文件 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) .pathMapping("/") .select() .paths(PathSelectors.regex("/.*")) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("我的接口文档") .contact(new Contact("名字", "url", "邮箱")) .description("这是我的swggerUi生成的接口文档") .version("版本") .build(); } }
原文:https://www.cnblogs.com/linxinmeng/p/12629673.html