提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
目录
提示:以下是本篇文章正文内容,下面案例可供参考
<!-- Knife4j依赖 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version> <!-- 请检查最新版本 -->
</dependency>
<!-- Swagger依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version> <!-- 请检查最新版本 -->
</dependency>
?DocumentationType是可配的,可以选择高版本或者低版本
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.finance.late.controller")) // 替换成你的Controller包路径
.paths(PathSelectors.any())
.build();
}
}
在启动类(带该注解的类 @SpringBootApplication)上添加注解:@EnableSwagger2
在类上添加注解
@Api(tags = "XXX模块")
public class LateController {
......
}
在接口上添加注解
@ApiOperation("XXXX方法")
@PostMapping ("/doSomething")
public Object doSomething(@RequestBody Object object) {
......
}
?在属性上添加注解
@Data
@ApiModel("XXXObject")
public class XXXObject implements Serializable {
private static final long serialVersionUID = 42L;
@ApiModelProperty(value = "xxx描述", required = false)
private String xxx;
@ApiModelProperty(value = "yyy描述", required = true)
private Object yyy;
}
该处required标明该字段是否必填
访问链接:http://IP:port/doc.html
如果对应的项目有类似的配置:server.servlet.context-path = /project_name
访问链接就对应为:http://IP:port/project_name/doc.html
?
没什么难度,简单到有手就行!