Spring Boot集成swagger
程序员文章站
2022-05-03 17:46:47
...
版本号
框架 | 版本号 |
---|---|
spring boot | 2.2.1.RELEASE |
swagger-ui | 2.6.1 |
引入maven依赖
引入swagger的maven依赖,可以引入新版本的swagge2r,具体使用方式没有大的变化,只是页面UI更加美观了
<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>
配置
添加java配置
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.ArrayList;
import java.util.List;
/**
* @Author: 俞兆鹏
* @Date: 2019/5/21 17:26
* @Email: aaa@qq.com
* @Version 1.0
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//扫描所有有注解的api,用这种方式更灵活
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()
.enable(true)
.useDefaultResponseMessages(false);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("代码生成平台API文档")
.description("接口由swagger2.6.1生成")
.termsOfServiceUrl("http://52.83.137.110")
.version("1.0.0")
.build();
}
}
使用方法
- 在controller上标记
import com.puhua.platform.entity.Test;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: 俞兆鹏
* @Date: 2019/12/2 16:00
* @Email: aaa@qq.com
* @Version 1.0
*/
@Api(description = "测试控制器", tags = {"HelloController"})
@RestController
@RequestMapping("/api")
public class HelloController {
@ApiOperation(value = "测试接口",notes= "这是一个测试接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "testParam", value = "这是一个测试参数(方法参数)", dataType = "string", paramType = "query", required = false),
})
@GetMapping("/hello")
public String hello(String testParam, Test testEntity){
return "hello";
}
}
- 实体类上标记
/**
* @author 俞兆鹏
* @since 2019-12-02
*/
@Data
@ApiModel(value="Test对象", description="实体类对象")
public class Test implements Serializable {
private static final long serialVersionUID=1L;
//在swagger上隐藏这个字段
@ApiModelProperty(value = "id",hidden = true)
private Integer id;
@ApiModelProperty(value = "姓名(实体类参数)")
private String name;
}
访问swagger地址
访问
http://localhost:8183/swagger-ui.html
备注
详细说明参考swagger官网