spring-boot 版本 2.7.7
1、引入Maven坐标 pom.xml
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2、修改配置 application.yml
spring:
mvc:
pathmatch:
matching-strategy: ANT_PATH_MATCHER
3、配置 Knife4j + Swagger
package com.example.demo.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@EnableKnife4j
@Slf4j
public class SwaggerConfig {
@Bean
public Docket docket() {
// 扫描指定接口所在路径
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
// swagger 信息
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("利用swagger2构建的API文档")
.description("用restful风格写接口")
.version("1.0")
.build();
}
}
4、创建实体类
package com.example.demo.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel("用户")
public class User {
@ApiModelProperty("主键")
private Integer id;
@ApiModelProperty("姓名")
private String name;
@ApiModelProperty("年龄")
private Integer age;
}
5、创建控制器
package com.example.demo.controller;
import com.example.demo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "用户相关接口")
public class UserController {
@GetMapping("/api/getUser")
@ApiOperation("获取用户信息")
@ApiImplicitParams(
@ApiImplicitParam(name="userId", value = "用户id", required = true, type = "String")
)
public User getUser(Integer userId) {
User user = new User();
user.setId(userId);
user.setAge(20);
user.setName("Tom");
return user;
}
}
浏览器打开: http://127.0.0.1:8080/doc.html
标签:Knife4j,swagger,Java,SpringBoot,documentation,import,springfox,com,annotations From: https://blog.51cto.com/mouday/6057462