1、先上项目配置好的swagger2的ui界面:
2、需要swagger2的这两个包:
<!-- swagger2 包 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${springfox-swagger2-version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${springfox-swagger2-version}</version> </dependency>
3、然后需要写配置swagger2的代码类:
代码内容如下:
package com.example.cloudorderdemo.config; import io.swagger.annotations.Api; import org.springframework.beans.factory.annotation.Value; 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; @Configuration public class SwaggerConfig {
//application.yml中配置的version @Value("${info.app.version}") private String version;
//application.yml中配置的应用名称 @Value("${spring.application.name}") private String applicationName; @Bean public Docket createRestApi(){//如果在application.yml中配置了项目访问路径userdemo就要在swagger访问路径的端口后面加上 /userdemo/ //最新:swagger的访问路径:http://localhost:8880/swagger-ui.html# return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(true) .select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("order订单服务") .version(version) .build(); } }
4、最后在application启动类上面加上注解:@EnableSwagger2
5、controller类上面写上接口swagger的注释:
类名上:
方法名上:
最后启动项目打开swagger文档地址:http://localhost:8882/swagger-ui.html#/
。
标签:springfox,springboot,documentation,swagger2,version,文档,import,swagger From: https://www.cnblogs.com/spll/p/16655263.html