第6节 springboot集成swaggerUI
自动生成接口文档并且测试接口
1.引入jar包
<properties>
<swagger.version>2.6.1</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
2.swagger配置文件
package com.course.config;
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("ella", "", "390442862@qq.com"))
.description("这是我的swaggerUI生成的接口文档")
.version("1.0.0.0")
.build();
}
}
重启springboot,访问http://localhost:8080/swagger-ui.html
没有拿到所写的接口信息,原因是接口中没有配置swagger配置
3.接口中配置swagger信息
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;
@RestController
@Api(value = "/", description = "这是我全部的get方法")
public class MyGetMethod {
@RequestMapping(value = "/getCookies", method = RequestMethod.GET)
@ApiOperation(value = "通过这个方法可以获取cookies", httpMethod = "GET")
public String getCookies(HttpServletResponse response) {
//HttpServerletRequest 装请求信息的类
//HttpServerletResponse 装响应信息的类
Cookie cookie = new Cookie("login", "true");
response.addCookie(cookie);
return "恭喜你获得cookies信息成功";
}
/**
* 这是一个需要携带cookies信息才能访问的get请求
*/
@RequestMapping(value = "get/with/cookies", method = RequestMethod.GET)
@ApiOperation(value = "这是一个需要携带cookies信息才能访问的get请求", httpMethod = "GET")
public String getWithCookies(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if (cookies == null) {
return "no cookies with get method";
}
for (Cookie cookie : cookies) {
if (cookie.getName().equals("login") && cookie.getValue().equals("true")) {
return "这是一个需要携带cookies信息才能访问的get请求";
}
}
return "cookies info wrong";
}
/**
* 开发一个需要携带参数才能访问的get请求
* 第一种实现方式:url:key=value&key=value
* 我们来模拟获取商品列表
*/
@RequestMapping(value = "/get/with/param", method = RequestMethod.GET)
@ApiOperation(value = "需要携带参数才能访问的get请求方法一", httpMethod = "GET")
public Map<String, Integer> getList(@RequestParam Integer start,
@RequestParam Integer end) {
Map<String, Integer> myLsit = new HashMap<String, Integer>();
myLsit.put("鞋子", 400);
myLsit.put("方便面", 5);
myLsit.put("口红", 300);
return myLsit;
}
/**
* 开发一个需要携带参数才能访问的get请求
* 第二种实现方式:url:url:port/get/with/param/10/20
* 我们来模拟获取商品列表
*/
@RequestMapping(value = "/get/with/param/{start}/{end}", method = RequestMethod.GET)
@ApiOperation(value = "需要携带参数才能访问的get请求方法二", httpMethod = "GET")
public Map<String, Integer> getMyList(@PathVariable Integer start,
@PathVariable Integer end) {
Map<String, Integer> myLsit = new HashMap<String, Integer>();
myLsit.put("鞋子", 400);
myLsit.put("方便面", 5);
myLsit.put("口红", 300);
return myLsit;
}
/**
* 开发一个需要携带参数和cookies才能访问的get请求
* 我们来模拟用户使用用户名和密码登录
*/
@RequestMapping(value = "/get/with/paramAndCookie", method = RequestMethod.GET)
@ApiOperation(value = "需要携带参数和cookies才能访问的get请求", httpMethod = "GET")
public String login(HttpServletRequest request,
@RequestParam String name,
@RequestParam String password) {
Cookie[] cookies = request.getCookies();
if (cookies == null) {
return "请配置cookie信息";
}
for (Cookie cookie : cookies) {
if (cookie.getName().equals("login") & cookie.getValue().equals("true")) {
if (name.equals("zhangsan") && password.equals("123456")) {
return "zhangsan 登录成功";
} else if (name.equals("zhangsan") && !password.equals("123456")) {
return "密码错误请重试";
} else {
return "请注册再登录";
}
}
}
return "请注册再登录";
}
}
配置swagger:
在类class加注释:@Api(value = "/", description = "这是我全部的get方法")
在方法中加注释:@ApiOperation(value = "需要携带参数和cookies才能访问的get请求", httpMethod = "GET")
重启springboot,再次访问http://localhost:8080/swagger-ui.html,仍旧没有接口信息,这个时候要注意springboot启动程序中要加载的路径是否包含swaggerUI配置文件的路径,经查看,不在一个路径下:
4. 配置springboot扫描的路径
配置springboot扫描的路径的时候要注意swagger和接口方法都在一个路径下即可,如@ComponentScan("com.course")
再次重启springboot,访问之前网址
5.测试接口
可以在该网站中对接口做简单的测试工作:找到某个具体的测试接口输入入参后,点击“Cry it out”
第7节 返回cookies信息的post接口开发
package com.course.server;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@RestController
@Api(value = "/", description = "这是我所有的post方法")
@RequestMapping(value = "/v1")
public class MyPostMethod {
//这个变量用来装我们的coookies信息
private static Cookie cookie;
//用户登录成功并获得cookis信息
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ApiOperation(value = "这是一个用户登录请求且登录成功后返回cookie信息", httpMethod = "POST")
public String login(HttpServletResponse response,
@RequestParam(value = "UserName", required = true) String name,
@RequestParam(value = "Password", required = true) String password) {
if (name.equals("zhangsan") && password.equals("123456")) {
cookie = new Cookie("login", "true");
response.addCookie(cookie);
return "登录成功";
}
return "用户名或密码错误";
}
}
重启springboot后,打开swaggerUI后
登录成功案例:
登录失败案例: