Swagger2和SpringBoot
买了好多本书,但是最近进新公司,学习新公司的技术是第一优先级.Swagger2以前有用过,忘了.今天在回顾一下吧.
码云地址: https://gitee.com/an-xing/swagger.git
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.anxing</groupId>
<artifactId>SwaggerDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
</project>
需要编写Swagger2的配置类
package com.anxing.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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;
import java.util.ArrayList;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置多个分组
@Bean
public Docket docket1(Environment environment){
//生产测试环境下有效
Profiles p = Profiles.of("dev", "test");
boolean b = environment.acceptsProfiles(p);
b=true;
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//配置分组
.groupName("安星2")
//是否启用swagger默认是true
.enable(b)
.select()
//RequestHandlerSelectors 配置要扫描接口的方式
//basePackage指定要扫描的包
//any扫描全部 none不扫描 withClassAnnotation 扫描类上的注解 withMethodAnnotation 扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.anxing.swagger.controller"))
// 指定路径的设置 any代表所有路径
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket docket(Environment environment){
//开发测试环境下有效
Profiles p = Profiles.of("dev", "test");
boolean b = environment.acceptsProfiles(p);
b=true;
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//配置分组
.groupName("安星")
//是否启用swagger默认是true
.enable(b)
.select()
//RequestHandlerSelectors 配置要扫描接口的方式
//basePackage指定要扫描的包
//any扫描全部 none不扫描 withClassAnnotation 扫描类上的注解 withMethodAnnotation 扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.anxing.swagger.controller"))
// 指定路径的设置 any代表所有路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
Contact contact = new Contact("安星","https://www.baidu.com","123456789@163.com");
return new ApiInfo("安星的接口文档",
"大神养成记",
"1.0",
"https://www.baidu.com",
contact,"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>());
}
}
Controller层代码
package com.anxing.swagger.controller;
import com.anxing.swagger.pojo.User;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api("测试类")
@RestController
@RequestMapping("anxing")
public class HelloController {
@GetMapping("axning")
public String add(){
return "你好啊";
}
@PostMapping("aaa")
public User addd(){
return new User("张三","123456");
}
}
jopo
package com.anxing.swagger.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
private String userName;
@ApiModelProperty("用户密码")
private String password;
public User(String userName, String password) {
this.userName = userName;
this.password = password;
}
}
常用注解
@Api:用在请求的类上,表示对类的说明
tags=“说明该类的作用,可以在UI界面上看到的注解”
value=“该参数没什么意义,在UI界面上也看到,所以不需要配置”
@ApiOperation:用在请求的方法上,说明方法的用途、作用
value=“说明方法的用途、作用”
notes=“方法的备注说明”
@ApiImplicitParams:用在请求的方法上,表示一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query --> 请求参数的获取:@RequestParam
· path(用于restful接口)–> 请求参数的获取:@PathVariable
· div(不常用)
· form(不常用)
dataType:参数类型,默认String,其它值dataType=“Integer”
defaultValue:参数的默认值
@ApiResponses:用在请求的方法上,表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:用于响应类上,表示一个返回响应数据的信息
(这种一般用在post创建的时候,使用@RequestBody这样的场景,
请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:用在属性上,描述响应类的属性
不管是开发还是写博文,技术和经验并不深厚,有不足的地方还请诸位指点