spring mvc如何集成swagger-ui2
发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,本篇文章给大家分享的是有关spring mvc如何集成swagger-ui2,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1·sprin
千家信息网最后更新 2025年12月01日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安全错误
数据库的锁怎样保障安全
网络安全靠人民手抄报小学生
如何进入连接容器中的数据库
手机下载软件开发商有记录吗
软件开发会不会做背调
CR 数据库什么意思
10086运维对数据库的要求
饥荒是用什么引擎软件开发的
excel数据库导入导出
服务器工程师成果
音频dlan服务器
政企专线业务网络安全要求
诛仙单机数据库
胶州软件开发
广告数据库设计
媒体网络安全ppt
广电网络技术性论文
陕西大数据服务器机箱厂家
榆林5g软件开发培训学校
现代网络技术期末试题
软件开发测试群名
省委网信办网络安全知识竞答题
网络技术学院奖获奖
Linux环境下软件开发
ntis数据库
gtasa 服务器
银行的软件开发笔试
下列各种对关系数据库
网络安全管理信息
苏州企业软件开发如何收费
天下捷融互联网科技小贷面试