共计 1304 个字符,预计需要花费 4 分钟才能阅读完成。
1. 导入依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> | |
<dependency> | |
<groupId>io.springfox</groupId> | |
<artifactId>springfox-swagger2</artifactId> | |
<version>3.0.0</version> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> | |
<dependency> | |
<groupId>io.springfox</groupId> | |
<artifactId>springfox-swagger-ui</artifactId> | |
<version>3.0.0</version> | |
</dependency> | |
<dependency> | |
<groupId>io.springfox</groupId> | |
<artifactId>springfox-boot-starter</artifactId> | |
<version>3.0.0</version> | |
</dependency> |
2. 创立配置类
@Configuration | |
@EnableSwagger2 | |
public class SwaggerConf { | |
@Bean | |
public Docket petApi() {return new Docket(DocumentationType.SWAGGER_2) | |
.apiInfo(apiInfo()) | |
.select() | |
.apis(RequestHandlerSelectors.basePackage("com.tao.swagger.controller")) // 指定提供接口所在的基包 | |
.build();} | |
/** | |
* 该套 API 阐明,蕴含作者、简介、版本、host、服务 URL | |
* @return | |
*/ | |
private ApiInfo apiInfo() {return new ApiInfoBuilder() | |
.title("demo api 阐明") | |
.contact(new Contact("allen","null","name@example.com")) | |
.version("0.1") | |
.termsOfServiceUrl("https://segmentfault.com") | |
.description("demo api") | |
.build();} | |
} |
留神配置类上的 @EnableSwagger2 注解
3. 编写测试 controller
@Api("hello controller 接口测试") | |
@Controller | |
public class HelloController {@ApiOperation("hello 接口,返回 hello success") | |
@GetMapping("/hello") | |
public String hello(){return "hello success";} | |
} |
关上 swagger 页面 http://localhost:8080/swagger-ui/index.html
正文完