springboot集成kafka_Spring Boot 集成Swagger

获得技能

  • 掌握SpringBoot集成Swagger技能
  • 掌握Swagger调试Web应用技能

本节对应教学视频

SpringBoot从入门到精通 SSM/MyBatis/Redis/KafKa/SpringCloud​ke.qq.com
v2-b51b2e914283b47e6284d0edd7efcd6e_180x120.jpg

知识要点

  • 使用@EnableSwagger2注解打开Swagger
  • 使用SwaggerUI调试Web应用

Swagger是一个非常方便的文档和测试工具,Swagger两大核心

  1. springfox-swagger2 核心
  2. springfox-swagger-ui UI工具方便显示

上机实验

Swagger集成到SpringBoot,并使用UI调试Web接口

核心步骤如下

  1. 引入Swagger依赖
  2. 使用@EnableSwagger2注解,新增SpringBoot配置类
  3. 浏览器打开SwaggerUi地址调试Web接口

引入Swagger依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
​
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
​

使用@EnableSwagger2注解,新增SpringBoot配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

浏览器打开SwaggerUi地址调试Web接口

#11000代表SpringBoot的端口 http://localhost:11000/v2/api-docs http://localhost:11000/swagger-ui.html

v2-321a3b140699f7a66ca7d25e07e4fb25_b.jpg

扩展资料

Swagger 静态文件放行

package com.fencaibc.springboot.edu;
​
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
​
/**
 * 作者 粉菜编程学院
 * 微信公众号 fencaibc
 * QQ号 2321484518
 **/
@Configuration
@EnableWebMvc
public class FreeMarkerConfig implements WebMvcConfigurer {
​
    
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //过滤swagger
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/swagger-resources/**")
                .addResourceLocations("classpath:/META-INF/resources/swagger-resources/");
        registry.addResourceHandler("/swagger/**")
                .addResourceLocations("classpath:/META-INF/resources/swagger*");
        registry.addResourceHandler("/v2/api-docs/**")
                .addResourceLocations("classpath:/META-INF/resources/v2/api-docs/");
    }
}
​
​
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值