依赖版本信息
Spring boot 2.1.0.RELEASE
swagger2 2.7.0
1. mvn 配置  pom.xml 包引入
 <!--swagger2依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!--swagger2美化插件依赖-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
2.配置Config
在Application.java同级目录创建 Swagger2Config.java 内容

 
package com.muyuer.springdemo;

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;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class Swagger2Config { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.muyuer.springdemo.controller"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XX系统_接口文档")
.description("XX系统,具体包括XXX,XXX模块...")
.contact(new springfox.documentation.service.Contact("muyuer", "http://www.615000.com", "182443947@qq.com"))
.version("版本号:1.0")
.build();
}
}

3.Swagger注解
swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @author by muyuer
* @date : 2018-11-30 14:22
*/
@Api("接口用途 XX接口")
@RestController
public class HelloController { @ApiOperation("描述方法用途 XX方法")
@GetMapping("/hello")
public String say(){
return "Hello SpringBoot!";
} @ApiOperation("描述方法用途 xx方法")
@ApiImplicitParam(name = "name", value = "XX参数", dataType = "String")
@GetMapping("/hello2")
public String say2(String name){
return name + ": Hello SpringBoot!";
}
}

4.遇到的问题

开始使用其它版本swagger出现过以下错误 
{"code":404,"message":"接口 [/swagger-ui.html] 不存在"}
网上搭建答案是
添加 WebMvcConfigurer.java内容如下 
//swagger2配置
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}

自己实际测试添加了这些代码并未生效

重新建了项目引用上面版本的Spring boot 2.1.0.RELEASE swagger2 2.7.0包后即成功了。

Spring boot 2.1.0 -- swagger2 整合的更多相关文章

  1. Spring Boot:Boot2.0版本整合Neo4j

    前面介绍了Boot 1.5版本集成Neo4j,Boot 2.0以上版本Neo4j变化较大. 场景还是电影人员关系 Boot 2.0主要变化 GraphRepository在Boot2.0下不支持了,调 ...

  2. Spring Boot项目中使用Swagger2

    Swagger2是一款restful接口文档在线生成和在线接口调试工具,Swagger2在Swagger1.x版本的基础上做了些改进,下面是在一个Spring Boot项目中引入Swagger2的简要 ...

  3. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

  4. 解决Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of the configured nodes are available

    Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of ...

  5. Spring Boot 2.5.0 发布:支持Java16、Gradle 7、Datasource初始化机制调整

    今年520的事情是真的多,娱乐圈的我们不管,就跟DD一起来看看 Spring Boot 2.5.0 的发布吧!看看都带来了哪些振奋人心的新特性和改动! 主要更新 支持 Java 16 支持 Gradl ...

  6. Spring Boot 2.1.0 已发布,7 个重大更新!

    距离<重磅:Spring Boot 2.0 正式发布!>已经过去大半年了,而 Spring Boot 2.1.0 在 10 月底就发布了,我们来看下 Spring Boot 2.1.0 都 ...

  7. (转)Spring Boot 2 (九):【重磅】Spring Boot 2.1.0 权威发布

    http://www.ityouknow.com/springboot/2018/11/03/spring-boot-2.1.html 如果这两天登录 https://start.spring.io/ ...

  8. 【重磅】Spring Boot 2.1.0 权威发布

    如果这两天登录 https://start.spring.io/ 就会发现,Spring Boot 默认版本已经升到了 2.1.0.这是因为 Spring Boot 刚刚发布了 2.1.0 版本,我们 ...

  9. Spring Boot 2.2.0新特性

    Spring Boot 2.2.0 正式发布了,可从 repo.spring.io 或是 Maven Central 获取. 性能提升   Spring Boot 2.2.0 的性能获得了很大的提升. ...

随机推荐

  1. Java DecimalFormat的主要功能及使用方法

    DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字.该类设计有各种功能,使其能够分析和格式化任意语言环境中的数,包括对西方语言.阿拉伯语和印度语数字的支持. ...

  2. pyHook监听用户鼠标、键盘事件

    一.代码部分:获取用户输入信息,并与截图一起保存到XX目录下   # -*- coding: utf-8 -*- #   import pythoncom import pyHook    impor ...

  3. apache安装方式

    1.首先需要下载apachi,apache_2.2.22.msi 2.双击安装包进行安装,安装过程中可能出现一些选择性问题,将重点部分截图如下: 说明:这三个内容就按如上输入即可. 说明:选择自定义安 ...

  4. 读CSV转换datatable

    using System.Data; using System.IO;   /// <summary> /// Stream读取.csv文件 /// </summary> // ...

  5. [Java学习] Java异常类型

    所有异常类型都是内置类Throwable的子类.因此,Throwable在异常类层次结构的顶层.紧接着Throwable下面的是两个把异常分成两个不同分支的子类.一个分支是Exception. 该类用 ...

  6. WPF如何给窗口Window增加阴影效果

    https://blog.csdn.net/w_sx12553/article/details/45072861

  7. Intent在Activity之间传值的几种方式

    发这篇博客主要讲一下Android中Intent中如何传值的几种方法: 1:基本数据类型,包含了Java八种基本数据类型和CharSequece文本2:八种数据类新对应数组和CharSequece文本 ...

  8. Hibernate中的HQL的基本常用小例子,单表查询与多表查询

    <span style="font-size:24px;color:#3366ff;">本文章实现HQL的以下功能:</span> /** * hql语法: ...

  9. 10046 event 知多少

    10046 event 知多少 2017年5月10日 10:08 1.在当前session级打开trace 适用于SQL语句可以在新的session创建后再运行. 在session级收集10046 t ...

  10. dp练习(9)——最大乘积

    1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Desc ...