高效接口文档swagger3来啦!!!但是它版本好高导致我处处碰壁!!
一、前言
为什么选择swagger,因为好用哇!为什么使用knife,因为好看哇!左边是knife右边是swagger,瞅瞅对比,明显knife非常高级非常好看!
其次,knife在swagger的基础上增加了文档说明的功能,使得接口相关情况更加直观,并且,knife整合了swagger,所以有了knife你就有了swagger!
二、引入依赖
上文说到,knife集成了swagger3,因此,在引入依赖的时候可以只引入knife。
<!-- knife4j -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<!-- swagger3 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
三、配置
1、在启动类XXXApplocation配置,添加注解@EnableSwagger2WebMvc
@Slf4j //日志
@ComponentScan(value = {"com.**.**"}) //扫描
@SpringBootApplication //启动
@EnableSwagger2WebMvc //文档配置
public class XXXApplication {
public static void main(String[] args) {
SpringApplication.run(XXXApplication.class, args);
}
}
2、配置SwaggerConfig,也是网页展示配置
@Configuration
//@EnableOpenApi // 开始swagger
@EnableKnife4j // 开启knife4j
public class SwaggerConfig {
//knife
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
//.title("swagger-bootstrap-ui-demo RESTful APIs")
.description("# swagger-bootstrap-ui-demo RESTful APIs")
.termsOfServiceUrl("http://www.xx.com/")
.version("1.0")
.build())
//分组名称
.groupName("2.X版本")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.ljj.demo.controller"))
.paths(PathSelectors.any())
.build();
}
}
这里因为我的springboot是用的最新版,出现了版本不兼容问题,导致我的接口访问不到,所以手动加了一个对外暴露接口(不知道是不是因为这个方法的原因,反正我是解决了问题),3.0版本的springboot到现在给我造成的问题还挺多的,所以没有特殊情况,直接降版本更省事。
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier,
EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,
WebEndpointProperties webEndpointProperties, Environment environment) {
List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping =
webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath)
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),
shouldRegisterLinksMapping, null);
}
四、使用
Controller层配置
@RestController
@RequestMapping("/demo")
@Api(tags = "用户模块")
//@DS("two")
public class DemoController {
@PostMapping("/userList")
@ApiOperation(value = "查询所有的用户信息")
public UserVO getUserList() {
UserVO vo = new UserVO();
vo.setUserId("nihao");
vo.setUserName("你好");
vo.setUserAge(18);
vo.setPassword("123456");
vo.setPhone("12345612345");
vo.setAddress("你好你好你好你好你好你好你好");
return vo;
}
}
实体类,@ApiModelProperty的标注会在参数说明中展示
@Data
public class UserVO {
@ApiModelProperty(value = "用户id")
private Integer userId;
@ApiModelProperty(value = "用户名")
private String userName;
@ApiModelProperty(value = "用户年龄")
private Intager userAge;
@ApiModelProperty(value = "用户密码")
private String password;
@ApiModelProperty(value = "手机号")
private String phone;
@ApiModelProperty(value = "用户地址")
private String address;
}
访问路径:
http://localhost:8080/doc.html(knife)、http://localhost:8080/swagger-ui/index.html(swagger)
哪怕我从头到尾讲的都是knife,但是swagger也可以用!
小白也要努力成长起来!欢迎大家指点和讨论!