Home > Java > javaTutorial > body text

How springboot integrates swagger3 and knife4j

王林
Release: 2023-05-19 12:49:06
forward
2018 people have browsed it

springboot integrates swagger3

swagger3’s springboot starter jar package

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
Copy after login

Note: The current SpringBoot version is 2.5.6, and Swagger3.0 is currently not fully compatible with SpringBoot2.6. x!

Write TestController code

@RestController
@RequestMapping("test")
public class TestController {
    @GetMapping
    public Map<String, Object> get(@RequestParam String id) {
        Map<String, Object> r = new HashMap<>(1);
        r.put("id", id);
        return r;
    }

    @PostMapping
    public Map<String, Object> post() {
        Map<String, Object> r = new HashMap<>(1);
        r.put("code", 200);
        return r;
    }
    @PutMapping
    public Map<String, Object> put(String id) {
        Map<String, Object> r = new HashMap<>(1);
        r.put("id", id);
        return r;
    }

    @DeleteMapping
    public Map<String, Object> delete(String id) {
        Map<String, Object> r = new HashMap<>(1);
        r.put("id", id);
        return r;
    }
}
Copy after login

Create Swagger3Configuration

@Configuration
@EnableOpenApi
public class SwaggerConfig {
    private static final String VERSION = "0.0.1";
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("分组名称")
                .apiInfo(apiInfo())
                .select()
                //要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 设置标题
                .title("文档标题")
                //联系人
                .contact(contact())
                //描述
                .description("xxx文档")
                //服务
                .termsOfServiceUrl("https://spring.io/")
                //许可证
                .license("Apache 2.0")
                .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0")
                .version(VERSION)
                .build();
    }
    private Contact contact (){
        return new Contact("SpringBoot", "https://spring.io/", "email");
    }
}
Copy after login

Annotate the interface

Commonly used annotations in swagger
##@Api Indicates the description of the class. Common parametersClass@ApiOperationDescribes the purpose of the methodMethod@ApiImplicitParamsIt can contain multiple @ApiImplicitParamMethod@ApiImplicitParamDescription Purpose of parametersMethod@ApiModelRepresents information of a data classClass@ApiModelPropertyDescribe the properties of the data classProperties@ApiIgnoreIgnore a field so that It is not displayed in the documentAttribute
AnnotationFunctionUse location
Basic use of the interface
1. Create a new user entity class

@ApiModel("用户")
@Data
public class User {
    @ApiModelProperty("用户名")
    private String username;
    @ApiModelProperty("密码")
    private String password;
}
Copy after login

2. Add annotations to TestController

@Api(tags = "测试接口")
@RestController
@RequestMapping("test")
public class TestController {
    @ApiOperation("get请求")
    @GetMapping
    @ApiImplicitParam(name = "id", value = "测试用id", dataTypeClass = String.class)
    public Map<String, Object> get(@RequestParam String id) {
        Map<String, Object> r = new HashMap<>(1);
        r.put("id", id);
        return r;
    }

    @ApiOperation("post请求")
    @PostMapping
    public Map<String, Object> post(@RequestBody User user) {
        Map<String, Object> r = new HashMap<>(1);
        r.put("code", 200);
        return r;
    }

    @ApiOperation("put请求")
    @PutMapping
    @ApiImplicitParam(name = "id", value = "put请求id", dataTypeClass = String.class)
    public Map<String, Object> put(String id) {
        Map<String, Object> r = new HashMap<>(1);
        r.put("id", id);
        return r;
    }

    @ApiOperation("delete请求")
    @DeleteMapping
    @ApiImplicitParam(name = "id", value = "delete请求id", dataTypeClass = String.class)
    public Map<String, Object> delete(String id) {
        Map<String, Object> r = new HashMap<>(1);
        r.put("id", id);
        return r;
    }
}
Copy after login

Running results

How springboot integrates swagger3 and knife4j

How springboot integrates swagger3 and knife4j

How springboot integrates swagger3 and knife4j##Integration Better UI-knife4j

maven

<dependency>
   <groupId>com.github.xiaoymin</groupId>
   <artifactId>knife4j-micro-spring-boot-starter</artifactId>
   <version>3.0.3</version>
</dependency>
Copy after login
Enabler
Add **@EnableKnife4j**
@Configuration
@EnableOpenApi
@EnableKnife4j
public class SwaggerConfig
Copy after login
on SwaggerConfig class

The above is the detailed content of How springboot integrates swagger3 and knife4j. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template