1:说明

网上这类文章 太多, 一搜一大把 ,但是要不是知识太过于老旧,就是配置没有说名清楚,你的项目按照他的配置却不能正常运行:

所以本文的目的: 配置swagger 2  那swagger 1 不说一下吗,我觉得没有必要了,确实需要以jar包方式构建 或者 维护老项目,那么参考下面的连接

https://github.com/swagger-api/swagger-ui/tree/2.x/dist  下载这个路径内容,导入相关依赖即可,不建议使用

2: swagger2 部署方式1

  2.1: 导入lib

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

  2.2 2: 创建配置类

@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.dgw.controller"))
// 扫描@APi 标记的Class
//.apis(RequestHandlerSelectors.withClassAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger2构建RESTful APIs")
.description(" 项目 ")
.contact(new Contact("dgw", "https://www.cnblogs.com/dgwblog/", "xxx@qq.com"))
.version("1.0")
.build();
}
}

  2.3 基本上到这里 网上那些教程让你启动 http://localhost:8080/swagger-ui.html# 访问查看,然后介绍API就完事了 ? 他难道没有用到 拦截器 Spring Boot 访问映射 ? 你开发项目 就是一个hello world? 哈哈

  下面你必须配置资源映射 sping boot 2 在webmvcconfigurationsupport中配置

/**
* 支持webjars
*/
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
/**
* 支持swagger
*/
// 解决 SWAGGER 404报错
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

  2.4 如果你的项目展示没有使用到拦截器 那么是可以成功访问的 ,但是最好知道需要配置拦截器

registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/**")
.excludePathPatterns("/user/login","/","/index")
// swagger 排除规则
.excludePathPatterns("/swagger-ui.html")
.excludePathPatterns("/swagger-resources/**")
.excludePathPatterns("/error")
.excludePathPatterns("/webjars/**");

这个时候 访问一下: 没有问题:

对了 如果你的项目用到 spring security 还需要排除以下配置

@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { @Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**");
} }

3: swagger2 部署方式2 推荐

  导入lib'

<!-- https://mvnrepository.com/artifact/com.spring4all/swagger-spring-boot-starter -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.0.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.xml配置

swagger.title=spring-boot-starter-swagger
swagger.description=Starter for swagger 2.x
swagger.version=1.4.0.RELEASE
swagger.license=Apache License, Version 2.0
swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger
swagger.contact.name=didi
swagger.contact.url=https://www.cnblogs.com/dgwblog/
swagger.contact.email=xxx@qq.com
# 扫描包路径
swagger.base-package=com.dgw.controller
swagger.base-path=/**

启动配置swagger 扫描

@SpringBootApplication 这个注解
@EnableSwagger2Doc
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

这里如果出现了 建议看前面的 文章 ,自己考虑一下 为什么不能访问.

这里写个测试

@Controller
@Api("接口说明")
public class HelloController {
@ApiOperation(value = "hello方法 ",notes = "返回index")
@GetMapping("/hello")
public String hello(){
return "index";
}
}

能够正常访问:

Spring Boot中使用Swagger2构建强大的RESTful(最新全,无坑)的更多相关文章

  1. Spring Boot中使用Swagger2构建强大的RESTful API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  2. Spring Boot 中使用 Swagger2 构建强大的 RESTful API 文档

    项目现状:由于前后端分离,没有很好的前后端合作工具. 由于接口众多,并且细节复杂(需要考虑不同的HTTP请求类型.HTTP头部信息.HTTP请求内容等),高质量地创建这份文档本身就是件非常吃力的事,下 ...

  3. Spring Boot中使用Swagger2构建API文档

    程序员都很希望别人能写技术文档,自己却很不愿意写文档.因为接口数量繁多,并且充满业务细节,写文档需要花大量的时间去处理格式排版,代码修改后还需要同步修改文档,经常因为项目时间紧等原因导致文档滞后于代码 ...

  4. Spring Boot中使用Swagger2构建RESTful APIs

    关于 Swagger Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因: Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API. S ...

  5. Spring Boot中使用Swagger2构建RESTful API文档

    在开发rest api的时候,为了减少与其他团队平时开发期间的频繁沟通成本,传统做法我们会创建一份RESTful API文档来记录所有接口细节,然而这样的做法有以下几个问题: 1.由于接口众多,并且细 ...

  6. Spring Boot中使用Swagger2构建RESTful APIs介绍

    1.添加相关依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <depen ...

  7. SpringBoot_06_使用Swagger2构建强大的RESTful API文档

    二.参考资料 1.Spring Boot中使用Swagger2构建强大的RESTful API文档 2.

  8. Spring Boot中使用Swagger2自动构建API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  9. Spring Boot教程(二十二)使用Swagger2构建强大的RESTful API文档(1)

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

随机推荐

  1. 伪共享 FalseSharing (CacheLine,MESI) 浅析以及Java里的解决方案

    起因 在阅读百度的发号器 uid-generator 源码的过程中,发现了一段很奇怪的代码: /** * Represents a padded {@link AtomicLong} to preve ...

  2. 开放下载 | 《Knative 云原生应用开发指南》开启云原生时代 Serverless 之门

    点击下载<Knative 云原生应用开发指南> 自 2018 年 Knative 项目开源后,就得到了广大开发者的密切关注.Knative 在 Kubernetes 之上提供了一套完整的应 ...

  3. nitacm第十六届浙江大学宁波理工学院程序设计大赛总结

    校赛时间:2019.11.30周六下午12:00-16:00 重现赛链接:https://ac.nowcoder.com/acm/contest/2995#question 体验: 11点多到达石鳞大 ...

  4. Atoder-3620

    The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to ...

  5. CSU OJ 2148 梦皮神

    Description Wells最近经常做一些有皮神出现的梦. 在这一次梦中Wells把皮神(Pikachu)弄丢了,Wells在一个正 N 边形区域的中心开始自闭,Wells想找回皮神,同时皮神也 ...

  6. k近邻聚类简介

    简介 在所有机器学习算法中,k近邻(K-Nearest Neighbors,KNN)相对是比较简单的. 尽管它很简单,但事实证明它在某些任务中非常有效,甚至更好.它可以用于分类和回归问题! 然而,它更 ...

  7. 深入理解 Java 反射和动态代理

  8. python学习-文件创建读取

    # 文件创建 # 读写# 文件存在?不存在?在操作系统上# 读 read r 写 write w# 打开一个文件# fs = open("xiaojian.txt",encodin ...

  9. python学习-caculator

    # 运算符操作# 算术运算符num_a = 100num_b = 5000 # 加法print(num_a + num_b)# 减法print(num_a - num_b)# 乘法 *print(nu ...

  10. 【Java】在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    题目描述: 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整 ...