ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Springboot整合swagger

2022-05-27 12:33:11  阅读:175  来源: 互联网

标签:springfox Springboot springframework 整合 org import swagger annotation


1:pom引入swagger的包

     <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>

2:添加swagger配置

package com.text.swagger.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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 读取配置文件中的开关
     */
    @Value("${swagger.enable:false}")
    private boolean enable;


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger测试")
                .description("")
                .contact(new Contact("swagger测试", "www.baidu.com", "1111111@qq.com"))
                .version("1.0.1")
                .build();
    }
    
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enable)
                .apiInfo(apiInfo())
                .select()
                //swagger扫描的包路径,注意:如果写的swagger API注解不在如下包内,则swagger页面不会显示  后面会添加controller进行演示
                .apis(RequestHandlerSelectors.basePackage("com.text.swagger.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }


}

3:在配置文件中,把swagger开关打开,

swagger.enable=true

如果不打开,swagger访问页面将会报错,如图

 

 

 

 

 

 

4:在controller中添加注解:包路径:com.text.swagger.demo.controller;

package com.text.swagger.demo.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
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.ResponseBody;

@Controller
@Api(tags = "swagger测试模块")//标题
@RequestMapping("/swagger")
public class SwaggerTextController {



    @ApiOperation("getSwaggerStr测试") //方法标题
    @ApiImplicitParams({
            @ApiImplicitParam(value = "值", name = "key", dataTypeClass = String.class, required = true) //方法参数注释
    })
    @GetMapping("/get")
    @ResponseBody
    public Object getSwaggerStr(@RequestParam String key){
        return "传入的值是:" + key;
    }
}

 

5:再添加一个包,不在swagger指定的com.text.swagger.demo.controller这个包下,包路径:com.text.swagger.demo.api;

 

package com.text.swagger.demo.api;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
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.ResponseBody;

@Controller
@RequestMapping("noswagger")
@Api(tags = "swagger不会显示的接口信息")
public class NoSwaggerController {



    @ApiOperation("不显示swagger接口信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "值", name = "key", dataTypeClass = String.class, required = true)
    })
    @GetMapping("/get")
    @ResponseBody
    public Object getSwaggerStr(@RequestParam String key){
        return "传入的值是:" + key;
    }

}

 

6:打开swagger地址:http://localhost:8016/swagger-ui.html#/

 

 

 7:如果整合过程中有如下报错

Failed to start bean 'documentationPluginsBootstrapper'; nested.....

解决思路是,改变springboot和swagger的版本,他们存在一些依赖关系

对应关系大概如下:

springboot:2.6.x  对应swagger 2..9.2版本

springbot:2.6.5 对应swagger:3.0.0

 

8:最后点击测试:成功响应

 

标签:springfox,Springboot,springframework,整合,org,import,swagger,annotation
来源: https://www.cnblogs.com/aiqingbi-aifeifei/p/16317162.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有