依赖版本信息
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 JDK5新特性-增强for

    2017-10-31 00:02:16 格式: for(元素数据类型 变量:数组或者Collection集合) { 使用变量即可,该变量即是元素 } int arr[] = {1,2,3,4,5}; ...

  2. HTML提交方式post和get区别(实验)

    HTML提交方式post和get区别(实验) 一.post和get区别 get提交,提交的信息都显示在地址栏中. post提交,提交的信息不显示地址栏中,显示在消息体中. 二.客户端代码 <!D ...

  3. ASCII码、ISO8859-1、Unicode、GBK和UTF-8 的区别

    为什么需要编码? 计算机中最小的存储单位是字节(byte),一个字节所能表示的字符数又有限,1byte=8bit,一个字节最多也只能表示255个字符,而世界上的语种又多,都有各种不同的字符,无法用一个 ...

  4. Mac安装软件时 提示已损坏的解决方法

    进入终端: sudo spctl --master-disable

  5. 3-22 Ruby 编码规则(个人整理)

    编码规则 https://github.com/thoughtbot/guides/tree/master/style/ruby *Use a trailing comma after each it ...

  6. CF576E Painting Edges

    首先,有一个很暴力的nk的做法,就是对每种颜色分别开棵lct来维护. 实际上,有复杂度与k无关的做法. 感觉和bzoj4025二分图那个题的区别就在于这个题是边dfs线段树边拆分区间.

  7. Eclipse 汉化方法

    1 打开 http://www.eclipse.org/babel/downloads.php 2  复制 http://download.eclipse.org/technology/babel/u ...

  8. asp.net GridView多行表头的实现,合并表头

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == Da ...

  9. create rootfs.img using loop device

    reference: https://www.thegeekdiary.com/how-to-create-virtual-block-device-loop-device-filesystem-in ...

  10. web 攻击靶机解题过程

    sql注入靶机攻击过程请参考 https://pentesterlab.com/exercises/from_sqli_to_shell/course http://www.sohu.com/a/12 ...