spring mvc如何集成swagger-ui2
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本篇文章给大家分享的是有关spring mvc如何集成swagger-ui2,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1·sprin
千家信息网最后更新 2026年10月31日spring mvc如何集成swagger-ui2
本篇文章给大家分享的是有关spring mvc如何集成swagger-ui2,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
1·spring mvc 构建 swagger-ui
1·添加依赖
com.google.guava guava 27.0.1-jre com.github.xiaoymin swagger-bootstrap-ui 1.9.6 io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0
2`添加swagger资源文件
3·拦截器放行swagger资源文件
4·swagger请求授权
5·创建swagger的配置类
package swagger;import io.swagger.annotations.ApiOperation;import org.springframework.context.annotation.Bean;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;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;//启用Swagger2@EnableSwagger2public class Swagger2Config extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()).select() //扫描指定包中的swagger注解 //.apis(RequestHandlerSelectors.basePackage("com.xia.controller")) //扫描所有有注解的api,用这种方式更灵活 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } @Bean private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("基础平台 RESTful APIs") .description("基础平台 RESTful 风格的接口文档,内容详细,极大的减少了前后端的沟通成本,同时确保代码与文档保持高度一致,极大的减少维护文档的时间。") .termsOfServiceUrl("http://xiachengwei5.coding.me") .version("1.0.0") .termsOfServiceUrl("http://xxx.xxx.com") .license("LICENSE") .licenseUrl("http://xxx.xxx.com") .build(); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController("/docApi/v2/api-docs", "/v2/api-docs"); registry.addRedirectViewController("/docApi/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui"); registry.addRedirectViewController("/docApi/swagger-resources/configuration/security", "/swagger-resources/configuration/security"); registry.addRedirectViewController("/docApi/swagger-resources", "/swagger-resources"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/docApi/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html"); registry.addResourceHandler("/docApi/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); }}6·加载swagger配置类
7·运行项目 访问地址:ip/项目名/doc.html
swagger常用注解:
@ApiIgnore 注解可以忽略这个类、方法、参数
@Api(tags={"这里写controller的注释"})
@Api(tags={"知识库接口"})@RestController@RequestMappingpublic class InfoController {@ApiOperation(value = "方法描述")
@ApiOperation(value = "获取某法律法规详情数据") @RequestMapping(value = "/info", method = RequestMethod.GET) public Info getInfoById(
@ApiParam(name="id",value = "方法参数描述")
public Info getInfoById( @ApiParam(name="id",value = "法律法规的ID") @RequestParam("id") String id)@ApiModelProperty(value="实体字段描述")
@ApiModelProperty(value="ID") private String id;
@ApiModel(value = "实体类描述")
@ApiModel(value = "info:法律法规实体")public class Info
以上就是spring mvc如何集成swagger-ui2,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
注解
实体
文档
方法
法律
法律法规
法规
知识
参数
基础
平台
接口
文件
更多
篇文章
详情
资源
项目
配置
实用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
瑰夏咖啡
河南真二互联网科技有限公司
互联网 科技创新
互联网科技公司电话
贷财互联网科技
四川狮子座互联网科技
盛创互联网科技有限公司
中正互联网科技招聘
济南和联互联网科技有限公司
互联网科技博主是什么意思
青岛科技大学互联网大赛研究生
互联网科技产业特征
新兴科技互联网创新大赛颁奖典礼
深圳育鹏互联网科技有限公司招聘
互联网金融科技展
美国互联网科技实力
厦门江牛科技互联网
抖集互联网科技技术咨询
六子台科技互联网展会
株洲百竞科技的互联网营销顾问
深圳恒合互联网络科技有限公司
互联网科技官网
鸠鸠互联网科技
青岛互联网科技公司
互联网科技论坛
无锡溥仪互联网科技有限公司
中国最先进互联网科技公司
科技互联网行业投资研究
西安左小互联网科技有限公司
银川新华互联网科技学校招聘