1.pom中添加swagger依赖

<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
<exclusions>
<exclusion>
<artifactId>javassist</artifactId>
<groupId>org.javassist</groupId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
<exclusions>
<exclusion>
<artifactId>javassist</artifactId>
<groupId>org.javassist</groupId>
</exclusion>
</exclusions>
</dependency>

2.编写配置类

@Configuration
@EnableSwagger2
public class SwaggerConfiguration { @Bean
public Docket createRestApi() {
System.out.println("====== SWAGGER CONFIG ======");
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("controller包的全路径名"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("xxxx RESTful APIs")
.description("快速上手,快速开发,快速交接")
.contact("xxxx")
.version("2.0.0")
.build();
} }

3.添加静态资源访问及权限拦截的放行(可选)

/**
* application configuration
*/
@Configuration
public class AppConfiguration implements WebMvcConfigurer { public static final String LOGIN_INTERCEPTOR_PATH_PATTERN = "/**/*";
public static final String LOGIN_PATH_PATTERN = "/login";
public static final String PATH_PATTERN = "/**";
public static final String LOCALE_LANGUAGE_COOKIE = "language";
public static final int COOKIE_MAX_AGE = 3600; @Bean
public LoginHandlerInterceptor loginInterceptor() {
return new LoginHandlerInterceptor();
} /**
* Cookie
* @return local resolver
*/
@Bean(name = "localeResolver")
public LocaleResolver localeResolver() {
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setCookieName(LOCALE_LANGUAGE_COOKIE);
/** set default locale **/
localeResolver.setDefaultLocale(Locale.US);
/** set cookie max age **/
localeResolver.setCookieMaxAge(COOKIE_MAX_AGE);
return localeResolver;
} @Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
/** **/
lci.setParamName("language"); return lci;
} @Override
public void addInterceptors(InterceptorRegistry registry) {
//i18n
registry.addInterceptor(localeChangeInterceptor()); registry.addInterceptor(loginInterceptor())
.addPathPatterns(LOGIN_INTERCEPTOR_PATH_PATTERN)
.excludePathPatterns(LOGIN_PATH_PATTERN,"/swagger-ui.html","/swagger-resources/**", "/webjars/**", "/v2/**", "/doc.html", "*.html", "/ui/**");
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/ui/**").addResourceLocations("file:ui/");
// 解决swagger无法访问
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
} @Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/ui/").setViewName("forward:/ui/index.html");
registry.addViewController("/").setViewName("forward:/ui/index.html");
} @Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping(PATH_PATTERN).allowedOrigins("*").allowedMethods("*");
} /**
* Turn off suffix-based content negotiation
*
* @param configurer configurer
*/
@Override
public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
} }

springboot项目添加swagger2的更多相关文章

  1. 为springboot项目添加springboot-admin监控

    我们知道spring-boot-actuator暴露了大量统计和监控信息的端点,spring-boot-admin 就是为此提供的监控项目. 先来看看大概会提供什么样的功能 从图中可以看出,主要内容都 ...

  2. springboot项目添加jsp支持

    一.创建springboot项目 使用 http://start.spring.io/ 快速创建一个springboot项目下载并导入 二.添加依赖 在pom.xml中添加支持jsp的依赖如下: &l ...

  3. springboot项目利用Swagger2生成在线接口文档

    Swagger简介. Swagger2是一款restful接口文档在线生成和在线调试工具.很多项目团队利用Swagger自动生成接口文档,保证接口文档和代码同步更新.在线调试.简单地说,你可以利用这个 ...

  4. springboot项目整合swagger2出现的问题

    swagger需要开放以下uri:/swagger-ui.html/swagger-resources/webjars/csrf/v2 添加swagger后项目报错 Failed to start b ...

  5. Springboot项目整合Swagger2报错

    SpringBoot2.2.6整合swagger2.2.2版本的问题,启动SpringBoot报如下错: Error starting ApplicationContext. To display t ...

  6. IDEA中springboot项目添加yml格式配置文件

    1.先创建application.properties 文件,在resources文件夹,右键 new -> Resource Bundle  如下图所示,填写名称 2.生成如下图所示文件 3. ...

  7. springboot 项目添加jaeger调用链监控

    1.添加maven依赖<dependency> <groupId>io.opentracing.contrib</groupId> <artifactId&g ...

  8. 完成一个springboot项目的完整总结-------二

    我们接着上篇继续写,继续进行springboot项目 一. swagger2 接口描述,测试每个接口是否有效 1. 添加依赖 pom.xml 在编辑pom.xml之前,要先关闭spring-boot项 ...

  9. 从零开始的SpringBoot项目 ( 五 ) 整合 Swagger 实现在线API文档的功能

    综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...

随机推荐

  1. src/众测篇:oracle注入过滤-- , + - * /,case when ,select,from,decode等函数如何证明是注入?

    (1)nullif: NULLIF:如果exp1和exp2相等则返回空(NULL),否则返回第一个值 真: 假: (2)nvl/nvl2 测试失败无法实现:)记录   (3)如果是oracle报错注入 ...

  2. G - Number Transformation(BFS+素数)

    In this problem, you are given an integer number s. You can transform any integer number A to anothe ...

  3. Python语言程序设计(笔记)

    1.平方根的格式化 知识点:平方根计算 pow(a,0.5)[可以计算负数,结果为复数] a**b 例题: 获得用户输入的一个整数a,计算a的平方根,保留小数点后3位,并打印输出.‪‬‪‬‪‬‪‬‪‬ ...

  4. Webshell和一句话木马

    目录 Webshell(大马) 一句话木马(小马) 一句话木马原理 一句话木马的变形 JSP后门脚本 Webshell(大马) 我们经常会看到Webshell,那么,到底什么是Webshell呢? w ...

  5. 【Mybtais】Mybatis 插件 Plugin开发(一)动态代理步步解析

    需求: 对原有系统中的方法进行'拦截',在方法执行的前后添加新的处理逻辑. 分析: 不是办法的办法就是,对原有的每个方法进行修改,添加上新的逻辑:如果需要拦截的方法比较少,选择此方法到是会节省成本.但 ...

  6. 测试中常用的链接URL----方便自己查找

    1.TesterHome:https://testerhome.com/ 2.selenium的操作手册:https://selenium-python.readthedocs.io/ 3.

  7. C++基于armadillo im2col的实现

    最近学习CNN,需要用到im2col这个函数,无奈网上没有多少使用armadillo的例子,而且armadillo库中似乎也没有这个函数,因此自己写了. im2col的原理网上一大把,我懒得写了. 1 ...

  8. 10.Debug

    1.Debug模式 1.1 什么是Debug模式 是供程序员使用的程序调试工具,它可以用于查看程序的执行流程,也可以用于追踪程序执行过程来调试程序. 1.2 Debug介绍与操作流程 Debug调式, ...

  9. table不能使用jQuery的slideDown的解决方法

    table不能使用jQuery的slideDown的解决方法 一个后台管理项目中遇到了如下场景 要求父级栏目可以收纳子栏目,即折叠功能,而且要有过渡动画,不能太生硬. 它是用table来实现的,但是遇 ...

  10. golang:并发编程总结

    并行和并发 并发编程是指在一台处理器上"同时"处理多个任务. 宏观并发:在一段时间内,有多个程序在同时运行. 微观并发:在同一时刻只能有一条指令执行,但多个程序指令被快速的轮换执行 ...