XML配置
1 2 3 4 5 6
| <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.9</version> </dependency>
|
Controller控制器Api
注解配置
类配置 :Api@Api(tags = "xxx")
方法配置 : @ApiOperation("xxx")
, @ApiOperationSupport(order = 1~n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @Slf4j
@Api(tags = "3.图片管理模块") @RestController @RequestMapping("/album") public class AlbumController { public AlbumController() { log.info("创建相册控制器对象.AlbumController"); } @Autowired private IAlbumService albumService;
public AlbumController(IAlbumService albumService) { log.info("创建相册控制器对象.AlbumController"); } @ApiOperation("添加相册") @ApiOperationSupport(order = 10) @PostMapping("/add-new") public String addNew(AlbumAddNewDTO albumAddNewDTO){ albumService.addNew(albumAddNewDTO); } }
|
Knife4jConfiguration配置类
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| @Configuration @EnableSwagger2WebMvc public class Knife4jConfiguration {
private String basePackage = "cn.tedu.jsdvn2203.csmall.server.controller";
private String groupName = "product";
private String host = "http://java.tedu.cn";
private String title = "酷鲨商城在线API文档--商品管理";
private String description = "酷鲨商城在线API文档--商品管理";
private String termsOfServiceUrl = "http://www.apache.org/licenses/LICENSE-2.0";
private String contactName = "Java教学研发部";
private String contactUrl = "http://java.tedu.cn";
private String contactEmail = "java@tedu.cn";
private String version = "1.0.0";
@Autowired private OpenApiExtensionResolver openApiExtensionResolver;
@Bean public Docket docket() { String groupName = "1.0.0"; Docket docket = new Docket(DocumentationType.SWAGGER_2) .host(host) .apiInfo(apiInfo()) .groupName(groupName) .select() .apis(RequestHandlerSelectors.basePackage(basePackage)) .paths(PathSelectors.any()) .build() .extensions(openApiExtensionResolver.buildExtensions(groupName)); return docket; }
private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(title) .description(description) .termsOfServiceUrl(termsOfServiceUrl) .contact(new Contact(contactName, contactUrl, contactEmail)) .version(version) .build(); }
}
|