依赖版本信息
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. STL_算法_02_排序算法

    ◆ 常用的排序算法: 1.1.合并(容器A(全部/部分)&容器B(全部/部分)==>容器C(全部/部分),容器C中元素已经排好顺序),返回的值==>iteratorOutBegin ...

  2. 【Golang 接口自动化08】使用标准库httptest完成HTTP请求的Mock测试

    前言 Mock是一个做自动化测试永远绕不过去的话题.本文主要介绍使用标准库net/http/httptest完成HTTP请求的Mock的测试方法. 可能有的小伙伴不太了解mock在实际自动化测试过程中 ...

  3. 网站链接facebook 拿新的post

    $http({ method: "GET", url: "https://graph.facebook.com/oauth/access_token?client_id= ...

  4. python模块——random模块(简单验证码实现)

    实现一个简单的验证码生成器 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" # Usage: 验证 ...

  5. 大年三十。让字母在屏幕上奔跑:(sleep , system"clear")

    system "clear",ruby清屏(osk系统上,window上用system "cls"). https://stackoverflow.com/qu ...

  6. codeforces 536a//Tavas and Karafs// Codeforces Round #299(Div. 1)

    题意:一个等差数列,首项为a,公差为b,无限长.操作cz是区间里选择最多m个不同的非0元素减1,最多操作t次,现给出区间左端ll,在t次操作能使区间全为0的情况下,问右端最大为多少. 这么一个简单题吞 ...

  7. EBS开发附件上传和下载功能(转)

    原文地址: EBS开发附件上传和下载功能 上传 Oracle ERP二次开发中使用的方式有两种,一是通过标准功能,在系统管理员中定义即可,不用写代码,就可以使几乎任何Form具有附件功能,具体参考系统 ...

  8. kubernetes资源清单入门

    创建nginx容器: kubectl run nginx-deploy8 --image=nginx:1.14-alpine --port=80 --replicas=2 deployment &qu ...

  9. elasticsearch term match multi_match区别

    转自:http://www.cnblogs.com/yjf512/p/4897294.html match 最简单的一个match例子: 查询和"我的宝马多少马力"这个查询语句匹配 ...

  10. oracleXE默认的管理员登录用户

    管理员: account:sys@XE as sysdba     pwd:sys sys@XE as sysdba             system