第一次接触swagger,网上各种折腾终于在项目里可以使用了,留一篇博客自我纪念一下.
Swagger的基本使用
一 : demo引入依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springboot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot tomcat 支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- thymeleaf legacyhtml5 模式支持 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<!-- 测试支持 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- tomcat的支持.-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- <!– mysql–>-->
<!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- <version>5.1.21</version>-->
<!-- </dependency>-->
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<!-- swagger- - >
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二 : Swagger的配置:
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration //表示这个类是一个配置类,会被加载
@EnableSwagger2//开启Swagger2
public class SwaggerConfig {
}
在配置类中写入Swagger的基本信息:
//配置文档信息
private ApiInfo apiInfo() {
Contact contact = new Contact();
return new ApiInfo(
"Swagger学习", // 标题
"学习演示如何配置Swagger", // 描述
"v1.0", // 版本
"http://terms.service.url/组织链接", // 组织链接
"contact", // 联系人信息
"Apach 2.0 许可", // 许可
"许可链接"// 许可连接
);
}
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(true)
.ignoredParameterTypes(HttpSession.class, HttpServletRequest.class, HttpServletResponse.class)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
//注意:.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
// basePackage("写你自己controller的包路径!!!!!!!!")
//swagger忽略HttpServletRequest参数.ignoredParameterTypes()
}
三 : 来个controller:
package com.example.demo.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.map.ApiJsonObject;
import com.example.demo.map.ApiJsonProperty;
import com.example.demo.model.DemoDoctor;
import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/say")
@Api(value = "SayController|一个用来测试swagger注解的控制器",tags = "实验一号")
public class SayController {
@ResponseBody
@RequestMapping(value = "/getUserName", method = RequestMethod.GET)
@ApiOperation(value = "单个Int测试", notes = "test: 仅1和2有正确返回")
@ApiImplicitParam(paramType = "query", name = "userNumber", value = "用户编号", required = true, dataType = "Integer")
public String getUserName(@RequestParam Integer userNumber) {
if (userNumber == 1) {
return "张三丰";
} else if (userNumber == 2) {
return "慕容复";
} else {
return "未知";
}
}
@ResponseBody
@PostMapping(value = "/updatePassword")
@ApiOperation(value = "修改用户密码", notes = "根据用户id修改密码")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "userId", value = "用户ID", required = true, dataType = "Integer"),
@ApiImplicitParam(paramType = "query", name = "password", value = "旧密码", required = true, dataType = "String"),
@ApiImplicitParam(paramType = "query", name = "newPassword", value = "新密码", required = true, dataType = "String")
})
public String updatePassword(@RequestParam(value = "userId") Integer userId, @RequestParam(value = "password") String password,
@RequestParam(value = "newPassword") String newPassword) {
if (userId <= 0 || userId > 2) {
return "未知的用户";
}
if (StringUtils.isEmpty(password) || StringUtils.isEmpty(newPassword)) {
return "密码不能为空";
}
if (password.equals(newPassword)) {
return "新旧密码不能相同";
}
return "密码修改成功!";
}
/**
* 测试无参数,返回list
*
* @return
*/
@PostMapping("GetGateWayCategory")
@ApiOperation(value = "无参数", notes = "测试无参数,返回list")
public List<DemoDoctor> GetGateWayCategory() {
ArrayList<DemoDoctor> list = new ArrayList<>();
DemoDoctor doctor = new DemoDoctor();
doctor.setId(1);
doctor.setName("测试员");
list.add(doctor);
DemoDoctor doctor1 = new DemoDoctor();
doctor1.setId(2);
doctor1.setName("管理员");
list.add(doctor1);
return list;
}
/**
* 测试map,swagger2.9.2,,只显示一个"string",
* @param params
* @return
*/
@GetMapping("GetProvinceEnum")
@ApiOperation(value = "map测试1",notes = "测试失败")
@ApiImplicitParam(name = "params" , paramType = "body",examples = @Example({
@ExampleProperty(value = "{'user':'id'}", mediaType = "application/json")
}))
public Map<String, String> GetProvinceEnum(@RequestParam Map<String, Object> params) {
HashMap<String, String> map = new HashMap<>();
map.put("管理员", "14");
return map;
}
@ApiOperation(value = "map测试2" ,httpMethod = "POST",notes = "测试成功!")
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ApiImplicitParams(
@ApiImplicitParam(paramType = "header", name = "authorization", value = "Tonken", required = true, dataType = "String")
)
public ResponseEntity<HashMap<String, String>> login(@ApiJsonObject(paramType="body",name = "json对象",value = {
@ApiJsonProperty(paramType="query",required = true,key="name",example = "张三",description = "姓名",type = "String"),
@ApiJsonProperty(paramType="query",required = false,key="age",example = "12",description = "年龄",type = "int")
}) @RequestBody JSONObject dto) {
HashMap<String, String> map = new HashMap<>();
map.put("管理员", "14");
return ResponseEntity.ok(map);
}
}
四 : 启动类:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
五 : 访问 :
http://localhost:8080/swagger-ui.html
大功告成
出现如下页面:
其中config和swagger主页的对应关系如下:
其中Controller 和model 与 swagger主页的对应关系如下:
将Swagger引入到项目里
发现我们项目
- 启动类直接继承了WebMvcConfigurationSupport,
就不需要配置类继承WebMvcConfigurationSupport了
2.同时启动类过滤器放行 /swagger-ui.html
3.我们项目端口和8080不一样, 启动访问Swagger访问项目的端口号
细节一 ,Long和Integer和Byte在Swagger的显示
我用的是2.9.2版本的
其中方法传入参数为Long和Integer时,Swagger的接口显示如图:
其中 Long为integer ( $ int64)
Integer为integer($ int32)
Byte:为integer($ int32)
minimum: -128
maximum: 127
Swagger常见错误
一: No mapping for GET /null/swagger-resources/configuration/ui
解决方法:
关闭之前开的浏览器,重新打开浏览器输入 http://localhost:8080/swagger-ui.html
二: 访问Swagger首页为白页
正常现象,第一次加载swagger会比较慢,此时正在加载中,博主等了一会好了(其实是抓耳挠腮,在网上上蹿下跳半天)
三: No mapping for GET /swagger-ui.html
SpringBoot使用Swagger2本来可以使用的,后来出现的异常No mapping for GET /swagger-ui.html,这个异常其实不用怎么解释,说白了就是找不到了。
遇到这种情况请先查找,最近你所添加继承了【WebMvcConfigurationSupport】的类
如果继承了WebMvcConfigurationSupport,则在配置文件在中配置的相关内容会失效,需要重新指定静态资源
需要重新指定swagger静态资源
public class SmsAdminApplication extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**", "/favicon.ico").addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
不行就换swagger的版本
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
四 :Multiple Dockets with the same group name are not supported. The following duplicate groups were discovered. default
ioc里bean重复了,博主是项目模块依赖一个Swagger模块,又导入Swageer的jar包报的错误,删除一个即可
五 :😱 Could not render e, see the console.
莫名其妙的出现,重启项目,浏览器也没清楚缓存,莫名其妙解决了…
使用Swagger当传入参数是Map类型解决方法
不知道是那篇博客的了
package com.example.demo.map;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiJsonObject {
ApiJsonProperty[] value(); //对象属性值
String name(); //对象名称
String paramType();//请求类型
}
package com.example.demo.map;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiJsonProperty {
String key(); //key
String example() default "";
String type() default "String"; //支持string 和 int
String description() default "";
String paramType() default "";
boolean required() default true;
}
package com.example.demo.map;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Optional;
import javassist.*;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ConstPool;
import javassist.bytecode.annotation.Annotation;
import javassist.bytecode.annotation.BooleanMemberValue;
import javassist.bytecode.annotation.IntegerMemberValue;
import javassist.bytecode.annotation.StringMemberValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ResolvedMethodParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ParameterBuilderPlugin;
import springfox.documentation.spi.service.contexts.ParameterContext;
import java.util.Map;
@Component
@Order //plugin加载顺序,默认是最后加载
public class MapApiReader implements ParameterBuilderPlugin {
@Autowired
private TypeResolver typeResolver;
@Override
public void apply(ParameterContext parameterContext) {
ResolvedMethodParameter methodParameter = parameterContext.resolvedMethodParameter();
if (methodParameter.getParameterType().canCreateSubtype(JSONObject.class) ||methodParameter.getParameterType().canCreateSubtype(Map.class) || methodParameter.getParameterType().canCreateSubtype(String.class)) { //判断是否需要修改对象ModelRef,这里我判断的是Map类型和String类型需要重新修改ModelRef对象
Optional<ApiJsonObject> optional = methodParameter.findAnnotation(ApiJsonObject.class); //根据参数上的ApiJsonObject注解中的参数动态生成Class
if (optional.isPresent()) {
String name = optional.get().name(); //model 名称
String paramType = optional.get().paramType(); //请求类型
ApiJsonProperty[] properties = optional.get().value();
parameterContext.getDocumentationContext().getAdditionalModels().add(typeResolver.resolve(createRefModel(properties, name))); //像documentContext的Models中添加我们新生成的Class
parameterContext.parameterBuilder() //修改Map参数的ModelRef为我们动态生成的class
.parameterType(paramType)
.modelRef(new ModelRef(name)).description("对象参数")
.name(name);
}
}
}
private final static String basePackage = "com.sean.demo04.swagger.model."; //动态生成的Class名
/**
* 根据propertys中的值动态生成含有Swagger注解的javaBeen
*/
private Class createRefModel(ApiJsonProperty[] propertys, String name) {
ClassPool pool = ClassPool.getDefault();
CtClass ctClass = pool.makeClass(basePackage + name);
try {
for (ApiJsonProperty property : propertys) {
ctClass.addField(createField(property, ctClass));
}
return ctClass.toClass();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据property的值生成含有swagger apiModelProperty注解的属性
*/
private CtField createField(ApiJsonProperty property, CtClass ctClass) throws NotFoundException, CannotCompileException {
CtField ctField = new CtField(getFieldType(property.type()), property.key(), ctClass);
ctField.setModifiers(Modifier.PUBLIC);
ConstPool constPool = ctClass.getClassFile().getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
Annotation ann = new Annotation("io.swagger.annotations.ApiModelProperty", constPool);
ann.addMemberValue("value", new StringMemberValue(property.description(), constPool));
ann.addMemberValue("type", new StringMemberValue(property.type(), constPool));
ann.addMemberValue("paramType", new StringMemberValue(property.paramType(), constPool));
ann.addMemberValue("required", new BooleanMemberValue(property.required(), constPool));
if (ctField.getType().subclassOf(ClassPool.getDefault().get(String.class.getName())))
ann.addMemberValue("example", new StringMemberValue(property.example(), constPool));
if (ctField.getType().subclassOf(ClassPool.getDefault().get(int.class.getName())))
ann.addMemberValue("example", new IntegerMemberValue(constPool,Integer.parseInt(property.example())));
// ann.addMemberValue("example", new IntegerMemberValue(Integer.parseInt(property.example()), constPool));
attr.addAnnotation(ann);
ctField.getFieldInfo().addAttribute(attr);
return ctField;
}
private CtClass getFieldType(String type) throws NotFoundException {
CtClass fileType = null;
switch (type) {
case "String":
fileType = ClassPool.getDefault().get(String.class.getName());
break;
case "int":
fileType = ClassPool.getDefault().get(int.class.getName());
break;
}
return fileType;
}
@Override
public boolean supports(DocumentationType documentationType) {
return true;
}
}
package com.example.demo.map;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/*************************************************************
* Description: 统一返回结果类
* Author: zhang shanming
* CreateTime: 2019/1/7
************************************************************/
@ApiModel
public class TelResult<T>{
// 状态码:1成功,其他为失败
@ApiModelProperty(value = " 状态码:1成功,0为失败")
public int code;
// 成功为success,其他为失败原因
@ApiModelProperty(value = "返回信息")
public String msg;
// 数据结果集
@ApiModelProperty(value = "数据结果集")
public T data;
public TelResult(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public TelResult(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
美化swagger
源码地址: https://gitee.com/zhang-zhiwei233/swagger
- 引入坐标
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>
- 编写配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* @author Andon
* 2021/11/10
*/
@EnableSwagger2
@Configuration
public class SwaggerConfig {
private static final String headerKey = "token"; //header参数的key
@Bean
public Docket createRestApi() {
// 添加header参数headerKey
ParameterBuilder parameterBuilder = new ParameterBuilder();
List<Parameter> parameterList = new ArrayList<>();
parameterBuilder.name(headerKey).description(headerKey)
.modelRef(new ModelRef("string")).parameterType("header")
.required(false).build();
parameterList.add(parameterBuilder.build());
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
//TODO 注意,这里写本地真实路径,否则会扫描不到
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(parameterList)
.apiInfo(new ApiInfoBuilder()
.title("spring-boot-util")
.description("")
.version("v1.0")
.contact(new Contact("", "", ""))
.license("")
.licenseUrl("")
.build());
}
}
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* @author Andon
* 2021/12/29
*/
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
/**
* 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
3.编写controller
import com.example.swagger.pojo.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@Api(tags = "测试")
@RestController
@RequestMapping("/api")
public class HelloController {
@ApiOperation(value = "sayHelo")
@GetMapping("/hello")
public ResponseEntity<String> hello(String name){
return ResponseEntity.ok("Hello"+name);
}
@PostMapping("save")
@ApiOperation(value = "保存", notes="新增结果保存", produces = "application/json")
@ApiResponses({
@ApiResponse(code=0,message="success"),
@ApiResponse(code=-1,message="系统异常"),
@ApiResponse(code=-10000,message="系统繁忙,请稍后再次操作"),
@ApiResponse(code=3005,message="新增商户失败"),
@ApiResponse(code=3009,message="该联系人邮箱已绑定商户:xxxxxx")
})
public BaseResponse batchSave(@RequestBody String req){
return null;
}
}