Swagger是一款好用的
Java接口文档工具
,引入Maven坐标之后就可以在项目启动之后,通过访问相应的地址可以看到生成好的接口文档。
一、页面效果1
**访问地址:**http://localhost:8081/swagger-ui.html
依赖方式1:
(只支持效果1的展示形式)
<!-- swagger -->
<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>1.18.18</version>
</dependency>
二、页面效果2
**访问地址:**http://localhost:8081/doc.html
依赖方式2:
(支持效果1 和 效果2 的展示形式)
<!-- swagger -->
<dependency>
<groupId>com.open.capacity</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
三、Swagger配置类
import io.swagger.annotations.ApiOperation;
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.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;
import java.util.HashSet;
import java.util.Set;
/**
* <p> @Title SwaggerConfig
* <p> @Description Swagger 配置类
*
* @author ACGkaka
* @date 2021/4/23 15:40
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket buildDocket() {
/// accessToken 请求头要求
// ParameterBuilder ticketPar = new ParameterBuilder();
// List<Parameter> parameters = new ArrayList<>();
// ticketPar.name("accessToken").description("accessToken")
// .modelRef(new ModelRef("string")).parameterType("header")
// .required(false).build();
// parameters.add(ticketPar.build());
Set<String> consumes = new HashSet<>();
consumes.add("multipart/form-data");
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// .apis(RequestHandlerSelectors.any())
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()
// .globalOperationParameters(parameters)
.consumes(consumes);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Demo System")
.description("Demo System API")
.version("V1.0")
.build();
}
}
四、接口注解形式
import com.demo.swagger.query.DemoQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
/**
* <p> @Title DemoController
* <p> @Description Controller 接口
*
* @author ACGkaka
* @date 2021/4/24 23:35
*/
@Api(tags = "接口类")
@RestController
@RequestMapping("/demo")
public class DemoController {
@ApiOperation("get接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "主键", dataType = "String", paramType = "json", required = true)
})
@GetMapping("/get")
public String get(String id) {
return id;
}
@ApiOperation("post接口")
@PostMapping("/post")
public DemoQueryParam post(@RequestBody DemoQueryParam param) {
return param;
}
}
五、POST 请求体注解
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* <p> @Title DemoQueryParam
* <p> @Description Demo 请求体
*
* @author ACGkaka
* @date 2021/4/24 23:41
*/
@Data
@ApiModel(description = "请求入参")
public class DemoQueryParam {
/**
* 主键
*/
@ApiModelProperty(value = "主键", required = true)
private String id;
/**
* 姓名
*/
@ApiModelProperty(value = "姓名", required = true)
private String name;
/**
* 年龄
*/
@ApiModelProperty("年龄")
private Integer age;
}
整理完毕,完结撒花~