最近项目里要用到SpringBoot + swagger,查了其他小伙伴们的资料,或多或少有点问题,在此我再梳理一遍。

1、maven依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath/>
</parent>

<dependencies>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>

<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>

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

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

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.8</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
<exclusions>
<exclusion>
<artifactId>commons-codec</artifactId>
<groupId>commons-codec</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.4.0</version>
</dependency>

<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>

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

<!-- mock dependency -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
</dependencies>

2、配置swagger,让swagger加载起来

@Configuration
@EnableSwagger2
//配置在哪些环境启用swagger,好用。一般情况下在生产环境,用禁用swagger的。对应pom.xml中的 profiles配置

@Profile({"dev","fat","uat","pro"})
public class SwaggerConfig extends WebMvcConfigurationSupport{

@Bean
public Docket createRestApi () {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("xxxx")
.select()
.apis(RequestHandlerSelectors.basePackage("com.yunda.data.controller"))
.paths(PathSelectors.any())
.build().apiInfo(getApiInfo());
}

private ApiInfo getApiInfo(){
return new ApiInfoBuilder()
.title("dsync-dispatcher api")
.description("xxxx api")
.version("1.0.0")
.contact(new Contact("suyujun", "", "suyujun0131@yundasys.com"))
.build();
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//这些配置要有,我遇到了如果不配置,swagger一直是 404的问题
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
// 解决 SWAGGER 404报错
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}

3.springboot启动主类

@SpringBootApplication
@EnableApolloConfig
@EnableRabbit
@ImportResource
@MapperScan(basePackages = {"xxxx.xxxx.xxxx"})
public class DsyncDispatcherApplication extends WebMvcConfigurationSupport {
private static final Logger LOGGER = LoggerFactory.getLogger(DsyncDispatcherApplication.class);

public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(DsyncDispatcherApplication.class, args);
ApplicationContextUtils.setApplicationContext(applicationContext);
}
}

4.看看效果吧


SpringBoot集成swagger2.0的更多相关文章

  1. SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

    1.pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...

  2. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

  3. SpringBoot集成Swagger2在线文档

    目录 SpringBoot集成Swagger2在线文档 前言 集成SpringBoot 登录接口文档示例 代码 效果 注解说明 总结 SpringBoot集成Swagger2在线文档 前言 不得不说, ...

  4. springboot 集成swagger2.x 后静态资源报404

    package com.bgs360.configuration; import org.springframework.context.EnvironmentAware; import org.sp ...

  5. SpringBoot集成Swagger2并配置多个包路径扫描

    1. 简介   随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...

  6. springboot集成swagger2报Illegal DefaultValue null for parameter type integer

    springboot集成swagger2,实体类中有int类型,会报" Illegal DefaultValue null for parameter type integer"的 ...

  7. SpringBoot集成Swagger2 以及汉化 快速教程

    (一) Swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件 (二)为什么使用Swagger 在现在的开发过程中还有很大一部分公司都是以口口相传的方式来进行 ...

  8. Springboot集成swagger2生成接口文档

    [转载请注明]: 原文出处:https://www.cnblogs.com/jstarseven/p/11509884.html    作者:jstarseven    码字挺辛苦的.....   一 ...

  9. springboot 集成swagger2

    使用Swagger 可以动态生成Api接口文档,在项目开发过程中可以帮助前端开发同事减少和后端同事的沟通成本,而是直接参照生成的API接口文档进行开发,提高了开发效率.这里以springboot(版本 ...

随机推荐

  1. TestNG系列(二)TestNG注解

    前言 TetsNG提供了很多注解,允许测试人员灵活地组织测试用例 一.@Test @Tets是TestNG的核心注解,被注解的方法,表示为一个测试方法. description属性 @Test(des ...

  2. Seata AT 模式启动源码分析

    从上一篇文章「分布式事务中间件Seata的设计原理」讲了下 Seata AT 模式的一些设计原理,从中也知道了 AT 模式的三个角色(RM.TM.TC),接下来我会更新 Seata 源码分析系列文章. ...

  3. HotStuff共识协议详解

    1. 前言 HotStuff提出了一个三阶段投票的BFT类共识协议,该协议实现了safety.liveness.responsiveness特性.通过在投票过程中引入门限签名实现了O(n)的消息验证复 ...

  4. 在 Xcode9 中自定义文件头部注释和其他文本宏

    在 Xcode9 中自定义文件头部注释和其他文本宏 . 参考链接 注意生成的plist文件的名称为IDETemplateMacros.plist 在plist文件里设置自己想要的模板 注意plist存 ...

  5. Dubbo初步

    Dubbo 介绍 : Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC 实现服务的输出和输入功能,可以和 Spring 框架无缝集成.Dubbo 框架,是基于容器 ...

  6. 第5节:Java基础 - 必知必会(下)

    第5节:Java基础 - 必知必会(下) 本小节是Java基础篇章的第三小节,主要讲述Java中的Exception与Error,JIT编译器以及值传递与引用传递的知识点. 一.Java中的Excep ...

  7. git 设置和取消指定域名代理 - git config proxy

    Firstly - Check Check if U have global .gitconfig file 检查是否有全局 .gitconfig 文件 Usually global .gitconf ...

  8. [golang] nats的消息传递模型介绍

    目录 nats的消息传递模型 What is NATS 主题式消息(Subject-Based Messaging) 发布订阅(Publish-Subscribe) 请求应答(Request-Repl ...

  9. unity3d 随机添加树木

    开放世界随机地图才是最重要的.. 随机生成树木 Terrain.terrainData //获取地形设置 terrainData.treePrototypes {get;set;} //获取或设置树木 ...

  10. tensorflow学习笔记——VGGNet

    2014年,牛津大学计算机视觉组(Visual Geometry Group)和 Google DeepMind 公司的研究员一起研发了新的深度卷积神经网络:VGGNet ,并取得了ILSVRC201 ...