SpringBoot集成SwaggerUI自动生成接口文档
一、在pom.xml文件里添加SpringBoot的引用配置,代码如下:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
二、在com.course包下新增config包,config包下新增SwaggerConfig配置类,代码如下:
package com.course.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
//固定写法
public class SwaggerConfig extends WebMvcConfigurationSupport {
//2.6.1版本配置
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//v1取的是注解里的value值
.pathMapping("v1")
.select()
//"/.*"是通过正则匹配方法的路径
.paths(PathSelectors.regex("/.*"))
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder().title("UserManager service API")
.contact("1012527160@qq.com")
.description("this is UserManager service API")
.version("1.0")
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/source/**").addResourceLocations("classpath:/static/");
//添加swagger-ui访问
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
四、在com.course.server包下修改MyGetMethod类和MyPostMethod类
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;
//@RestController表示我是需要被扫描的类
@RestController
//接口文档注解
@Api(value = "v1",description = "这是我全部的get方法")
public class MyGetMethod {
//设置访问路径和请求方法
@RequestMapping(value = "/getCookies",method = RequestMethod.GET)
//接口文档描述
@ApiOperation(value = "通过这个方法可以获取到cookies",httpMethod = "Get")
//HttpServerletRequest 装请求信息的类
//HttpServerletResponse 装响应信息的类
public String getCookies(HttpServletResponse response){
Cookie cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你获得cookies成功2";
}
/**
* 要求客户端携带cookies访问
* 这是一个需要携带cookies信息才能访问的get请求
*/
@RequestMapping(value = "/get/with/cookies",method = RequestMethod.GET)
//接口文档描述
@ApiOperation(value = "要求客户端携带cookies访问",httpMethod = "Get")
//HttpServerletRequest 装请求信息的类
//HttpServerletResponse 装响应信息的类
public String getWithCookies(HttpServletRequest request){
//获取cookies
Cookie[] cookies = request.getCookies();
//判断cookies是否为空
if (Objects.isNull(cookies)){
return "你必须携带cookies信息来访问";
}
//判断cookies是否正确
for (Cookie cookie : cookies){
if (cookie.getName().equals("login") &&
cookie.getValue().equals("true"))
return "访问成功";
}
return "你必须携带正确的cookies信息来访问";
}
/**
* 开发一个需要携带参数才能访问的get请求
* 第一种实现方式 url: key=value&key=value
* 我们来模拟获取商品列表
*/
@RequestMapping(value = "/get/with/param",method =RequestMethod.GET )
//接口文档描述
@ApiOperation(value = "需要携带参数才能访问的get请求1",httpMethod = "Get")
//@RequestParam注解设置开始位置和结束位置
public Map<String,Integer> getlist(@RequestParam Integer start,
@RequestParam Integer end){
//定义商品列表
Map<String,Integer> myList = new HashMap<>();
//填入商品
myList.put("鞋",400);
myList.put("干脆面",1);
myList.put("衬衫",300);
return myList;
}
/**
* 第二种需要携带参数访问的get请求
* url:ip:port/get/with/param/10/20
*/
@RequestMapping(value = "/get/with/param/{start}/{end}")
//接口文档描述
@ApiOperation(value = "需要携带参数才能访问的get请求2",httpMethod = "Get")
public Map myGetList(@PathVariable Integer start,
@PathVariable Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",400);
myList.put("干脆面",1);
myList.put("衬衫",300);
return myList;
}
}
package com.course.server;
import com.course.bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.Cookie;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
//接口文档注解
@Api(value = "v1",description = "这是我全部的post请求")
//访问地址前要加/v1
@RequestMapping("/v1")
public class MyPostMethod {
//这个变量是用来装我们cookies信息的
private static Cookie cookie;
//用户登录成功获取到cookies,如何再访问其他接口获取到列表
@RequestMapping(value = "/login",method = RequestMethod.POST)
//接口文档描述
@ApiOperation(value = "登陆接口,成功后获取cookies信息",httpMethod = "post")
public String login(HttpServletResponse response,
@RequestParam(value = "userName",required = true) String userName,
@RequestParam(value = "password",required = true) String password){
if (userName.equals("zhangsan")&&password.equals("123456")){
cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你登陆成功";
}
return "用户名或者密码错误!";
}
@RequestMapping(value = "/getUserList",method = RequestMethod.POST)
//接口文档描述
@ApiOperation(value = "获取用户列表",httpMethod = "POST")
public String getUserList(HttpServletRequest request,
@RequestBody User u){
//声明对象
User user;
//获取cookies
Cookie[] cookies = request.getCookies();
//验证cookies是否合法
for (Cookie c : cookies){
if (c.getName().equals("login")
&& c.getValue().equals("true")
&& u.getUserName().equals("zhangsan")
&& u.getPassword().equals("123456")
){
user = new User();
user.setName("lisi");
user.setAge("18");
user.setSex("man");
return user.toString();
}
}
return "参数不合法";
}
}
五、启动Application类,在浏览器输入http://127.0.0.1:8081/swagger-ui.html