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@EnableSwagger2public 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 接口测试")@Controllerpublic class HelloController { @ApiOperation("hello 接口,返回hello success") @GetMapping("/hello") public String hello(){ return "hello success"; }}
关上swagger页面http://localhost:8080/swagger-ui/index.html