swagger2使用教程(swagger2配置)

swagger2使用教程(swagger2配置)程序员某天的对话 阿媴 阿猿 阿袁 你好 阿猿 最近在干嘛 还能干嘛 不是在 code 就是想方设法学习 code 就这 学习是件很简单的事情呀 打量着阿袁 似乎有什么话对他说 你是哪一个 guo 你们认识吗 阿媴 阿猿 阿袁 p p p br p p 这部分没有别的内容 只看行动 p

大家好,我是讯享网,很高兴认识大家。



swagger2.9.2教程 与swagger3.0.0教程_spring
讯享网

程序员某天的对话

 阿媴 阿猿 阿袁 

讯享网

你好!阿猿, 最近在干嘛?

还能干嘛,不是在code就是想方设法学习code?

就这?

学习是件很简单的事情呀

打量着阿袁…似乎有什么话对他说…

你是哪一个(guo),你们认识吗?

讯享网 阿媴

 阿猿

 阿袁
</p><p> <br></p><p>这部分没有别的内容,只看行动!<br></p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190532_641054fcdc26326249.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_spring_02' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p>程序员那点事就是实践实践再实践</p><ol><li>准备一个经典的工程</li></ol><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190533_641054fd058d765242.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_开发语言_03' title="在这里插入图片描述" style="width: 909px; visibility: visible;"></p><ol start="2"><li>导入swagger相关包</li></ol><p>swagger的相关包</p><p><strong>如果是使用swagger_2版本那么就用下面的swagger版本就可以,</strong></p><p>注意:这里要使用springboot的2.5及以下版本, ----但是这涉及到漏洞的问题,所以最好还是使用2.7.1(最新版本)</p><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><p>swagger_2 版本的相关的依赖明细<br></p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190533_641054fd2451257055.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_spring_04' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p><br></p><p> 使用springboot2.5运行<br></p><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><ol start="3"><li>配置swagger</li></ol><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><ol start="4"><li>运行工程</li></ol><p>访问Url----&gt;&gt;http://localhost:8888/swagger-ui.html</p><p>swagger界面功能介绍–&gt;&gt;<br></p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190533_641054fd3fdd339016.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_spring boot_05' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p><br></p><p> 访问时的文件存放位置—&gt;&gt;(swagger-ui 2.9.2 与3.0.0访问地址发生了一些变化<br></p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190533_641054fd5635474614.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_开发语言_06' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p>在这里我们在配置文件中实际上什么配置也没有写,我们来看一下,如何配置swagger</p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190533_641054fd868d259576.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_ci_07' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p><br></p><p> 我们手动配置一下<br></p><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190533_641054fdaa4b19753.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_开发语言_08' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p><br></p><p><strong>这里切换到swagger 3.0版本</strong></p><p> 如果是使用swagger3.0版本那么可以使用spring提供的swagger3的启动包,方便快捷,同时也可以配合使用最新版本的springboot版本,我想着反正最后都要升级成swagger 3.0.0,不如就直接肝swagger3.0.0,<br></p><p> 反正变动不大,<br></p><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190533_641054fdc4c2b68047.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_ci_09' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p>引入一个启动依赖即可–springfox-boot-starter<br></p><p> 然后启动工程----&gt;&gt;http://localhost:8888/swagger-ui/index.html<br></p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190533_641054fddd72096704.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_spring_10' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p>在controller层,我们可以看到各种请求方式,在测试的时候也很方便<br></p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190534_641054fe0734033131.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_开发语言_11' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><ol start="5"><li>可选接口展示----过滤</li></ol><p>默认是把所有接口都扫描了,但是我们有时只需要提供部分接口即可—&gt;&gt;</p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190534_641054fe2d12765370.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_spring boot_12' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><ol start="6"><li>关闭swagger扫描</li></ol><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190534_641054fe543f767680.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_java_13' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p><strong>swagger在开发环境/测试环境启用,在生产环境不启用</strong></p><p>首先我们在发布时一般不会将swagger相关代码移除,而是选择通过enable(false)的方式进行关闭swagger,</p><p>所以解决思路是<br> 1,判断是否是生产环境----<br> 2,关闭swagger</p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190534_641054fe7317e32846.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_开发语言_14' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p>代码如下—</p><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><p>传入一个–Environment (接口)对象,该接口下提供了四种方法,有一个已经被废弃了,</p><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><p>getActiveProfiles与getDefaultProfiles底层是一样的<br></p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190534_641054fe9def852988.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_开发语言_15' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p>acceptsProfiles要传入Profiles(接口)对象,里面由static 方法<br></p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190534_641054feb74cf19422.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_spring_16' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p><br></p><p> 整体的演示代码–&gt;&gt;<br></p><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><ol><li>设置多个分组</li></ol><p><strong>设置多个Docket即可</strong></p><p> 我们可以配置一些不同的粗略来满足不同的需求<br></p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190534_641054fecf42666846.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_spring boot_17' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><ol start="8"><li>设置实体类</li></ol><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><p>在model层—&gt;&gt;卡一看到变化----加了备注</p><p style="text-align:center;"><img   src='https://s2.51cto.com/images/blog/202303/14190535_641054ff016924585.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184'  alt='swagger2.9.2教程 与swagger3.0.0教程_java_18' title="在这里插入图片描述" style="width: 916px; visibility: visible;"></p><p>swagger一些常用注解<br></p><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><p><strong>@ApiImplicitParams:描述接口参数 的使用</strong></p><div data-card-loading="true" data-card-editable="false" data-syntax="plain"></div><p>                <br></p>
小讯
上一篇 2025-05-24 09:25
下一篇 2025-06-14 21:03

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/160858.html