Springboot 集成swagger3

Springboot 集成swagger3

1.引入springboot、swagger依赖

 <parent>
        <artifactId>spring-boot-dependencies</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.3.9.RELEASE</version>
  </parent>
  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
      <!--LomBok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
      
      <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.18</version>
       </dependency>
      <!-- swagger3-->
        <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>
            <exclusions>
                <exclusion>
                    <artifactId>swagger-models</artifactId>
                    <groupId>io.swagger</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>swagger-annotations</artifactId>
                    <groupId>io.swagger</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>guava</artifactId>
                    <groupId>com.google.guava</groupId>
                </exclusion>
            </exclusions>
        </dependency>
      
     </dependencies>

2.swagger 启动配置

import cn.hutool.core.net.NetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Set;

@Slf4j
@Component
public class StartUtil {

    private final ApplicationContext applicationContext;

    public StartUtil(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public void logStartInfo() {

        // 打印启动信息
        String serverPort = applicationContext.getEnvironment().getProperty("server.port", "8080");
        String serverPath = applicationContext.getEnvironment().getProperty("server.servlet.context-path", "");

        Set<String> set = NetUtil.localIpv4s();
        String localIp = NetUtil.LOCAL_IP;
        for (String s : set) {
            // 不使用本机地址,也不使用本机网关地址
            if (!localIp.equals(s) && !s.endsWith(".1")) {
                localIp = s;
                break;
            }
        }
        String apiUrl = String.format("http://%s:%s%s", localIp, serverPort, serverPath);

        log.info("application swagger started at    {}/doc.html#plus", apiUrl);
        log.info("application start successful!");

        /*
         * 注册shutdown钩子,当应用程序正常关闭时,会调用这里的方法
         * kill -9时,不会触发这段代码
         */
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            log.info("stop application successfully");
        }));
    }
}
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.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.nio.charset.StandardCharsets;

/**
 * Swagger配置
 * @author 
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {


    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("swagger").description("swagger").version("1.0.0").build();
    }

    /**
     * 项目中配置从pom中读取swagger配置
     * spring读取配置文件默认采用ISO8895编码,但pom中为UTF8编码,使用此方法进行转换
     */
    private String convert(String text) {
        return new String(text.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
    }
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class StartRunner implements ApplicationRunner {
    private final StartUtil startUtil;


    public StartRunner(StartUtil startUtil) {
        this.startUtil = startUtil;
    }

    @Override
    public void run(ApplicationArguments args) {
        startUtil.logStartInfo();
    }

}

3.swagger注解

1)启动类中添加 @EnableOpenApi 注解

2)controller 添加 @Api(tags=“接口分类名称”)注解

3)接口上添加 @ApiOperation(“接口名称”) 注解

3)请求响应参数添加 @Schema(name = “字段名”, description = “字段描述”)

启动后的ui界面
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值