SpringBoot进行整合Swagger2从此接口文档说拜拜

作者 : 开心源码 本文共3552个字,预计阅读时间需要9分钟 发布时间: 2022-05-14 共358人阅读

SpringBoot进行Swagger的整合非常方便我们进行接口对接和调试。接口文档也不用给前台提供了,简直酸爽。。。。。

我们首先需要引入Swagger的Jar包

一、依赖

 <!--整合swagger-->        <!-- swagger -->        <dependency>            <groupId>io.springfox</groupId>            <artifactId>springfox-swagger2</artifactId>            <version>${swagger.version}</version>        </dependency>        <!-- swagger-ui -->        <dependency>            <groupId>io.springfox</groupId>            <artifactId>springfox-swagger-ui</artifactId>            <version>${swagger.version}</version>        </dependency>

二、Swagger配置类

这个配置类你需要知道哪些东西能进行配置,我们可以抽出配置项,写到配置文件中,方便以后进行更改。 特别要注意的是里面配置了api文件也就是controller包的路径,不然生成的文档扫描不到接口。

package com.herbert.demo.config;import org.springframework.beans.factory.annotation.Value;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.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;/** * Created by Herbert on 2020/4/15. */@Configurationpublic class SwaggerConfig {    @Value("${swagger.enabled}")    private boolean enabled;    @Value("${swagger.title}")    private String title;    @Value("${swagger.base-package}")    private String basePackAge;    @Value("${swagger.description}")    private String description;    @Bean    public Docket createRestApi() {        return new Docket(DocumentationType.SWAGGER_2)                .apiInfo(apiInfo())                .select()                .apis(RequestHandlerSelectors.basePackage(basePackAge))                .paths(PathSelectors.any())                .build();    }    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个    private ApiInfo apiInfo() {        if(enabled){            return new ApiInfoBuilder()                //页面标题                .title(title)                //形容                .description(description)                .build();        }        return new ApiInfoBuilder().build();    }}

在SpringBoot中用@Configuration注解该类,等价于XML中配置beans;用@Bean标注方法等价于XML中配置bean。

我们通过@Value将外部的值动态注入到Bean中,读取配置中的文件信息

#swagger配置swagger:  enabled: true  title: 你知道吗?我是Title  description: 形容:不想写形容.  base-package: com.herbert.demo.controller

三、开启Swagger配置

Application.class 加上注解@EnableSwagger2 表示开启Swagger

package com.herbert.demo;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import springfox.documentation.swagger2.annotations.EnableSwagger2;/** * Created by Herbert on 2020/4/15. */@SpringBootApplication@MapperScan("com.herbert.demo.mapper") //扫描的mapper@EnableSwagger2public class Application {    public static void main(String[] args) throws Exception {        SpringApplication.run(Application.class, args);    }}

四、Restful 接口加入注解

package com.herbert.demo.controller;import com.herbert.demo.entity.Test;import com.herbert.demo.service.TestService;import io.swagger.annotations.Api;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;/** * Created by Herbert on 2020/4/15. */@Api(description = "测试接口")@RestController@RequestMapping("/test")@Slf4jpublic class TestController {    @Autowired    private TestService testService;    @ApiOperation(value = "查询测试" ,  notes="查询客户信息")    @ApiImplicitParams({            @ApiImplicitParam(name = "id", value = "客户ID", required = true, paramType = "query", dataType = "String")    })    @RequestMapping(value="getTest",method= RequestMethod.GET)    public Test GetUser(String id){        return testService.findTest(id);    }}

启动SpringBoot项目,访问 http://localhost:8089/swagger-ui.html

imageimageimage

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » SpringBoot进行整合Swagger2从此接口文档说拜拜

发表回复