Springboot集成Swagger3

需要使用到哪些依赖呢?

项目pom.xml中下添加swagger-annotations和springfox-boot-starter

              <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-annotations</artifactId>
                <version>1.5.20</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>3.0.0</version>
            </dependency>

有哪些配置文件呢?

两个配置类Swagger3Configuration 和SwaggerProperties

import org.springframework.context.annotation.Configuration;
import springfox.documentation.oas.annotations.EnableOpenApi;

@EnableOpenApi
@Configuration
public class Swagger3Configuration {
//http://localhost:8083/cloud-test/swagger-ui/index.html 地址
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("swagger")
public class SwaggerProperties {
   /**
    * 是否开启swagger,生产环境一般关闭,所以这里定义一个变量
    */
   private Boolean enable;

   /**
    * 项目应用名
    */
   private String applicationName;

   /**
    * 项目版本信息
    */
   private String applicationVersion;

   /**
    * 项目描述信息
    */
   private String applicationDescription;

   /**
    * 接口调试地址
    */
   private String tryHost;

   public Boolean getEnable() {
       return enable;
   }

   public void setEnable(Boolean enable) {
       this.enable = enable;
   }

   public String getApplicationName() {
       return applicationName;
   }

   public void setApplicationName(String applicationName) {
       this.applicationName = applicationName;
   }

   public String getApplicationVersion() {
       return applicationVersion;
   }

   public void setApplicationVersion(String applicationVersion) {
       this.applicationVersion = applicationVersion;
   }

   public String getApplicationDescription() {
       return applicationDescription;
   }

   public void setApplicationDescription(String applicationDescription) {
       this.applicationDescription = applicationDescription;
   }

   public String getTryHost() {
       return tryHost;
   }

   public void setTryHost(String tryHost) {
       this.tryHost = tryHost;
   }
}

application.yml有没有搞事情?

本人自己工程配置了port和context-path,大家按照自己的来

server:
  port: 8083
  servlet:
      context-path: /cloud-test

hello world搞一个

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@Api("test part")
public class HelloController {

    @RequestMapping("/hello")
    @ApiOperation(value = "hello", httpMethod = "GET", response = String.class)
    public String sayHello() {
        return "hello world!";
    }
}

浏览器输入:http://localhost:8083/cloud-test/swagger-ui/index.html
如下
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值