Swagger3.0和Knife4j

1、pom依赖


    <properties>
        <java.version>1.8</java.version>
        <spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version>
        <spring.boot.version>2.3.12.RELEASE</spring.boot.version>
        <spring.cloud.version>Hoxton.SR12</spring.cloud.version>
    </properties>
    
    <repositories>
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>https://maven.aliyun.com/nexus/content/repositories/central/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>https://maven.aliyun.com/nexus/content/repositories/central/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

 		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
             <version>2.3.12.RELEASE</version>
        </dependency>
        <!-- SpringBoot整合springfox-swagger3 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- knife4j -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>
        <!-- swagger3 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

2、configuration

2.1、Knife4jConfiguration

package org.wuwangfu.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author: jcshen
 * @Date: 2022/11/15
 * @Description:Knife4j
 *
 *  接口文档界面:http://localhost:8080/doc.html#
 */

@Configuration
@EnableKnife4j
public class Knife4jConfiguration {

    //knife
    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("Knife4j渲染OpenAPI接口文档")
                        .description("# Knife4j渲染OpenAPI接口文档")
                        //.termsOfServiceUrl("http://www.xx.com/")
                        // .contact(new Contact("jcshen","027","wuwangfu.com"))
                        .version("1.0")
                        .build())
                //分组名称
                .groupName("3.X版本")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("org.wuwangfu.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

2.2、SwaggerConfig

package org.wuwangfu.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author: jcshen
 * @Date: 2022/11/15
 * @Description:配置Swagger 3.0,网页展示配置
 *
 *  接口文档界面:http://localhost:8080/swagger-ui/index.html
 */


@EnableOpenApi   // 开启Swagger自定义接口文档
@Configuration   // 相当于Spring配置中的<beans>
public class SwaggerConfig {
    @Bean   // 相当于Spring 配置中的<bean>
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }
    // API基础信息定义(就是更新Swagger默认页面上的信息)
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger3接口文档")
                .description("Swagger3接口文档")
               // .contact(new Contact("jcshen", "网址", "wuwangfu.com"))
                .version("v1.0")
                .build();
    }


}

3、启动类KnifeApplication

package org.wuwangfu;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author: jcshen
 * @Date: 2022/11/15
 * @Description:
 */

@Slf4j  //日志
@SpringBootApplication  //启动
@EnableSwagger2 //文档配置
public class KnifeApplication {

    public static void main(String[] args) {
        SpringApplication.run(KnifeApplication.class,args);
    }

}

4、entity

package org.wuwangfu.entity;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author: jcshen
 * @Date: 2022/11/15
 * @Description:
 */

@Data
public class UserVO {

    @ApiModelProperty(value = "用户id")
    private String userId;
    @ApiModelProperty(value = "用户名")
    private String userName;
    @ApiModelProperty(value = "用户年龄")
    private Integer userAge;
    @ApiModelProperty(value = "用户密码")
    private String password;
    @ApiModelProperty(value = "手机号")
    private String phone;
    @ApiModelProperty(value = "用户地址")
    private String address;


}

5、controller

5.1、DemoController

package org.wuwangfu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wuwangfu.entity.UserVO;

/**
 * @author: jcshen
 * @Date: 2022/11/15
 * @Description:
 *  接口文档界面:http://localhost:8080/swagger-ui/index.html
 *
 */

@RestController
@RequestMapping("/demo")
@Api(tags = "demo模块")
public class DemoController {

    @GetMapping("/userList")
    @ApiOperation(value = "查询所有的用户信息")
    public UserVO getUserList() {
        UserVO vo = new UserVO();
        vo.setUserId("nihao");
        vo.setUserName("你好");
        vo.setUserAge(18);
        vo.setPassword("123456");
        vo.setPhone("12345612345");
        vo.setAddress("你好你好你好你好你好你好你好");
        return vo;
    }
}

5.2、UserController

package org.wuwangfu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wuwangfu.entity.UserVO;

/**
 * @author: jcshen
 * @Date: 2022/11/15
 * @Description:
 *  接口文档界面:http://localhost:8080/doc.html#
 *
 */

@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {

    @GetMapping("/userList")
    @ApiOperation(value = "查询所有的用户信息")
    public UserVO getUserList() {
        UserVO vo = new UserVO();
        vo.setUserId("nihao");
        vo.setUserName("你好");
        vo.setUserAge(18);
        vo.setPassword("123456");
        vo.setPhone("12345612345");
        vo.setAddress("你好你好你好你好你好你好你好");
        return vo;
    }
}

6、接口访问

swagger接口文档界面:http://localhost:8080/swagger-ui/index.html
knife接口文档界面:http://localhost:8080/doc.html#

Swagger 3与Knife4j整合的步骤如下: 首先,您需要在Maven依赖中添加以下两个依赖: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> ``` 接下来,您需要引入Swagger-UI的页面美化依赖(可选): ``` <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.8.5</version> </dependency> ``` 然后,您需要创建一个配置类,用于配置SwaggerKnife4j的相关信息。 最后,您可以使用Swagger的注解来定义API文档。 请注意,如果您使用的是Spring Boot的版本大于2.3,则还需要引入javax.validation的依赖: ``` <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency> ``` 这样,您就完成了Swagger 3与Knife4j的整合。通过访问Swagger的UI页面,您可以查看和测试API文档。123 #### 引用[.reference_title] - *1* [Swagger 3集成knife4j(第三方Swagger-UI插件)](https://blog.csdn.net/qq_38974638/article/details/120840743)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *2* [Spring Boot整合Swagger3.0Knife4j](https://blog.csdn.net/cristianoxm/article/details/128329539)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *3* [Swagger 3 整合 knife4j](https://blog.csdn.net/m0_52462015/article/details/120952625)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值