SpringBoot集成Swagger

只需三步

第一步:添加依赖
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
第二步:编写Swagger的配置类
package com.felix.firstdemo.config; // 换成你自己的路径

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi
@EnableWebMvc
public class SwaggerConfig {
    /**
     * 用于读取配置文件 application.properties 中 swagger 属性是否开启
     */
    @Value("${swagger.enabled}")
    Boolean swaggerEnabled;

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                //.apiInfo(apiInfo())
                // 是否开启swagger
                .enable(swaggerEnabled)
                .select()
                // 过滤条件,扫描指定路径下的文件,这里要换成你自己的controller的路径
.apis(RequestHandlerSelectors.basePackage("com.felix.firstdemo.controller"))
                // 指定路径处理,PathSelectors.any()代表不过滤任何路径
                //.paths(PathSelectors.any())
                .build();
    }
}
第三步:在application.yml文件中开启swagger
swagger:
  enabled: true

集成完毕,下面就可以使用了
控制器上面用 @Api(tags = "控制器描述")
接口上面用 @ApiOperation(value = "日志测试")
其他的注解的使用看看这篇文章:
https://blog.csdn.net/JokerLJG/article/details/123544934

package com.felix.firstdemo.controller;

import com.felix.firstdemo.pojo.School;
import com.felix.firstdemo.pojo.SchoolDto;
//import com.felix.firstdemo.utils.LogUtil;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
@Api(tags = "一个叫hello的控制器")
public class HelloController {

    @ApiOperation(value = "hello")
    @GetMapping("/hello")
    public String hello() {
        return "hello felix";
    }

    @ApiOperation(value = "日志测试")
    @GetMapping("/log")
    public String log(@RequestParam(name = "logType", required = false) String logType) {
        String result = "debug";
        if (!logType.isEmpty()) {
            result = logType;
        }
        String message = "response is ; " + result;
        return result;
    }

    @ApiOperation(value = "lombok测试")
    @GetMapping("/lom")
    public String lombok(SchoolDto school) {
        if (school == null)
            return "null";

        School result = new School();
        result.setSchoolId(school.getSchoolId());
        result.setSchoolName(school.getSchoolName());
        result.setEnglishName(school.getEnglishName());
        result.setAddress(school.getAddress());
        result.setBeef(school.isBeef());
        result.setLevel(school.getLevel());

        return result.toString();
    }

}

启动项目访问 。。。/swagger-ui/index.html
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值