Swagger是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。目标是使客户端和文件系统作为服务器以同样的速度来更新文件的方法,参数和模型紧密集成到服务器。
这个解释简单点来讲就是说,swagger是一款可以根据resutful风格生成的生成的接口开发文档,并且支持做测试的一款中间软件。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
启动时遇到一个 空指针 的bug,查了一下可能是因为我的 springboot 和 swagger 版本不匹配,百度了之后只能降springboot的版本或者升级swagger的版本。
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置作者信息
public static final Contact contact = new Contact("wyh", "xxx", "wyh8421@163.com");
@Bean
public Docket getDocket(){
//先选择版本,一共有三个版本,选择swagger2版本
//后面的 apiInfo 是自定义的 api 的详细信息:Docket apiInfo(ApiInfo apiInfo)
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
//我们自定义我们的api信息
public ApiInfo apiInfo(){
//public ApiInfo(String title, String description, String version,
// String termsOfServiceUrl, Contact contact, String license, String licenseUrl,
// Collection<VendorExtension> vendorExtensions)
return new ApiInfo("wyh的测试", "现在开始测试!","v 1.0","https://blog.csdn.net/weixin_45886859?spm=1000.2115.3001.5343",contact,
"Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}
效果:http://localhost:1126/swagger-ui.html

Springboot2.6.x以上的避免空指针异常,在启动类加注解@EnableWebMvc
访问接口文档页面404的,可以配置一个实现WebMvcConfigurer的类
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(“/").addResourceLocations(“classpath:/static/”);
/
* 配置swagger-ui显示文档
*/
registry.addResourceHandler(“swagger-ui.html”)
.addResourceLocations(“classpath:/META-INF/resources/”);
registry.addResourceHandler(”/webjars/**")
.addResourceLocations(“classpath:/META-INF/resources/webjars/”);
}}
@Bean
public Docket getDocket(){
//先选择版本,一共有三个版本,选择swagger2版本
//后面的 apiInfo 是自定义的 api 的详细信息:Docket apiInfo(ApiInfo apiInfo)
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.apiInfo(apiInfo())
.enable(true)
.select()
//根据方法上的注解进行扫描:在页面上只显示被 GetMapping 注解修饰的方法
// .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
//根据类上的注解进行扫描:在页面上只显示被 RequestMapping 注解修饰的类
.apis(RequestHandlerSelectors.withClassAnnotation(RequestMapping.class))
//扫描所有
// .apis(RequestHandlerSelectors.any())
//根据包进行扫描
// .apis(RequestHandlerSelectors.basePackage("com.swaggerdemo.controller"))
//拦截指定的资源
// .paths(PathSelectors.ant("/SwaggerConfig/**"))
.build();
}
@Bean
public Docket DocketInit1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("a");
}
@Bean
public Docket DocketInit2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("b");
}
@Bean
public Docket getDocket(){
//先选择版本,一共有三个版本,选择swagger2版本
//后面的 apiInfo 是自定义的 api 的详细信息:Docket apiInfo(ApiInfo apiInfo)
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(true)
.groupName("wyh")
.select()
.build();
}
通过不同的组名显示不同的页面作者信息

@RestController
@RequestMapping("/")
public class MyController {
@ApiOperation(value = "hello呀")
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String myMethod(){
return "hello";
}
@ApiOperation(value = "hello的测试呀")
@GetMapping("/test")
public String testMethod(){
return "test2";
}
@ApiOperation(value = "hello的测试呀")
@PostMapping("/user")
public user userLogin(@ApiParam("姓名") String name,String password){
user user = new user(name,password);
return user;
}
}



