SpringBoot integrates Swagger to implement online interface documentation
- Create a SpringBoot project, build with maven.
- Add the following dependencies to the pom file
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
- Write a configuration class
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.naihe.swagger_test.controller."))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("swagger集成测试")
.contact(new Contact("naihe", "http://www.itnaihe.club:8081", "[email protected]"))
.description("欢迎访问接口文档,这是描述信息")
.version("1.0").build();
}
}
- Used in Controller
@RestController
@RequestMapping("/details")
@Api(value = "详情相关业务接口", tags = "详情业务相关的controller")
public class DetailsController {
@GetMapping("/getDetails")
@ApiOperation(value = "xxx", name = "xxx")
public String getDetails(){
return "details";
}
}
Common annotations and their function descriptions