swagger2入门
swagger2入门
1.idea新建springboot项目





2.引入swagger2的jar包

1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.0</version> </dependency>
|
3.写swagger2的配置类
新建包configuration,包下新建类Swagger2Config


Swagger2Config的内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.example.demo.configuration;
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;
@Configuration //表示这是一个配置类 @EnableSwagger2 //启用Swagger2相关功能 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo"))//基础路径 .paths(PathSelectors.regex("/*"))//匹配开启接口文档的路径 .build(); }
private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("学习swaggerRestful API") .description("学习swaggerRestful API") .termsOfServiceUrl("http://127.0.0.1:8080/") .contact("demo") .version("1.0") .build(); }
}
|
4.写一个测试类
新建包controller,包下新建类TestSwagger2
类的内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.example.demo.controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
@Controller public class TestSwagger2 {
@RequestMapping public String test(){ return "swagger2"; } }
|
