![SpringFox swagger2 and SpringFox swagger2 UI 接口文档生成与查看 SpringFox swagger2 and SpringFox swagger2 UI 接口文档生成与查看](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
依赖:
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
使用:在你要生成接口处加上注解@EnableSwagger2 ,此处是整个项目都要,不建议这么玩, 建议加在controller上
@SpringBootApplication
@EnableSwagger2
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
自定义标题:配置:(这个可以不配置)
@Configuration
@EnableSwagger2
public class Swagger2Config { @Bean
public Docket createRestApi() {
// ParameterBuilder ticketPar = new ParameterBuilder();
// List<Parameter> pars = new ArrayList<Parameter>();
// ticketPar.name("Authorization").description("登录校验")//name表示名称,description表示描述
// .modelRef(new ModelRef("string")).parameterType("header")
// .required(false).defaultValue("Bearer
// ").build();//required表示是否必填,defaultvalue表示默认值
// pars.add(ticketPar.build());//添加完此处一定要把下边的带***的也加上否则不生效
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage(com.test.move.user.controller)) //指定暴露的AIP接口
.paths(PathSelectors.any()).build();
// .globalOperationParameters(pars);
} private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("XXX api 文档").description("这是XXX API 文档的说明") //自定义标题和说明
.termsOfServiceUrl("http://www.XXX.com/") //指定访问的URL
.contact(new Contact("xiaowang", "", "xiaowang@163.com")).build();
}
}
方法描述: 在方法上加上 @ApiOperation
@ApiOperation(value="这是一个上传excel的方法")
@PostMapping("/uploadExcel")
public CommonResponse uploadOrderExcel(@RequestParam("ePlusExcel") MultipartFile ePlusExcel,
)
效果:
参数描述:1. 参数是 包装类 或者模型 如PartDO 在属性上加 @ApiModelProperty(value="")
@ApiModelProperty(value="part 的 描述")
private String partDesc;
参数描述:2. 参数是 普通类型 如String name 在属性上加@ApiParam(value="")
@GetMapping("/queryInventory")
public CommonResponse queryInventory(@ApiParam(value="客户编码")String customerCode) {
效果:
查看: http://localhost:9999/swagger-ui.html