知识点
相关文章
更多最近更新
更多Swagger UI 快速入门-springmvc 整合Swagger UI 教程
2019-03-10 23:20|来源: 网路
现在很多开发团队都是前端开发与服务端分离,前端直接调用服务端写好接口。
如果你是服务端开发人员,也许你要做以下工作:
1、写接口文档,描述每个接口的功能,请求参数和返回结果
2、要自测自己所写的接口
解决上面两个问题,也许你之前的做法是
1、建一个wiki平台或文档,让前端自己去查看
2、使用单元测试或postman测试
有了Swagger UI ,会让你从中解放出来,在写代码的时候,就顺便把上面的事情给解决了
Swagger UI一个无依赖的HTML、JS和CSS集合,可以为Swagger兼容API动态生成优雅文档
先来看下面几张效果图
是不是和postman有点像,是不是方法的描述、请求参数、返回值都有了。这都是怎么做到的?
官方第一手资料:
Swagger UI 官网: http://swagger.io/swagger-ui/
Swagger UI 源码地址: https://github.com/swagger-api/swagger-ui
由于项目中使用的是springmvc,所以只测试了Swagger UI结合springmvc的例子
先添加maven依赖,spring其他依赖在这里就不做多说
<dependency> <groupId>com.mangofactory</groupId> <artifactId>swagger-springmvc</artifactId> <version>1.0.2</version> </dependency>
Swagger整合spring的配置类,此类会注入SpringSwaggerConfig,并配置Swagger拦截的路径、页面展示的信息等
package com._656463.swagger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import com.mangofactory.swagger.configuration.SpringSwaggerConfig; import com.mangofactory.swagger.models.dto.ApiInfo; import com.mangofactory.swagger.plugin.EnableSwagger; import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin; @Configuration @EnableSwagger @EnableWebMvc @ComponentScan("com._656463.biz.*.controller") public class ApiSwaggerConfig { private SpringSwaggerConfig springSwaggerConfig; /** * Required to autowire SpringSwaggerConfig */ @Autowired public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) { this.springSwaggerConfig = springSwaggerConfig; } /** * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc * framework - allowing for multiple swagger groups i.e. same code base * multiple swagger resource listings. */ @Bean public SwaggerSpringMvcPlugin customImplementation(){ return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(".*?"); } private ApiInfo apiInfo(){ ApiInfo apiInfo = new ApiInfo( "My Apps API Title", //页面的标题 "My Apps API Description", "My Apps API terms of service", "My Apps API Contact Email", "My Apps API Licence Type", "My Apps API License URL"); return apiInfo; } }
在spring配置文件中,声明这个bean,让其初始化(因为ApiSwaggerConfig我是放在一个特殊的包中,上线后直接去掉这个bean配置,让其不能被访问)
<bean class="com._656463.swagger.ApiSwaggerConfig" />
在controller中使用swagger ui
package com._656463.biz.web.controller.datatransfer; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; ....... import com.wordnik.swagger.annotations.ApiOperation; import com.wordnik.swagger.annotations.ApiParam; @Controller @RequestMapping("/datatransfer") public class DataTransferController { @Resource private DataTransferFacade transferFacade; @ApiOperation(value = "根据订单ID同步数据", httpMethod = "GET", response = ResponseDto.class, notes = "syncByOrderId") @RequestMapping(value = "syncByOrderId", method = RequestMethod.GET) @ResponseBody public ResponseDto syncByOrderId(@ApiParam(required = true, name="orderId", value = "订单ID") @RequestParam String orderId,Integer type) { 。。。。。。 } }
@ApiOperation是配置该的一些信息,方便在页面展示,还有请求方式等
@ApiParam 是对参数的一些描述
在页面怎么展示呢,这是需要页面的,页面直接在Swagger UI 的github上下载最新的源码,然后把dist下面的所有文件拷贝到你的项目中,当然也可以放在其他能访问的web项目里。
https://github.com/swagger-api/swagger-ui/releases/tag/v2.2.5
例如我拷贝到我项目的webapp/docapi下
项目启动后,Swagger UI的访问地址是http://ip:host[/项目]/api-docs,所以把下载下来的url改为你项目的,然后请求回json数据让其前端代码解析
到此,就全部配置完毕
访问http://localhost:8067/docsapi/index.html就出现前面那个页面效果了
相关问答
更多-
播放Swagger UI网址别名(Play Swagger UI url alias)[2022-06-02]
您不能在路径文件中为URL /docs/swagger-ui/index.html?url=/assets/swagger.json因为index.html是由swagger-ui plugin生成到public目录并且需要访问文件附近(如js和css文件)。 Play swagger-ui使用javascript通过URL参数获取基于json的路由描述,以进一步将此文档解析为swagger-ui,在您的情况下是/assets/swagger.json端点。 我试图制作swagger的index.html文 ... -
Swagger UI描述(Swagger UI Description)[2022-04-05]
你正在寻找参数的描述,而不是终点。 如果您需要在任何DataContract属性上显示说明,则应在其上添加DescriptionAttribute : [DataContract(Name = "book")] public class MyContract { [DataMember(Name = "UserName")] [Description("Name of the user")] public string UserName{ get; set; } // [. ... -
我正在使用另一个应用程序来模仿它的服务,它可能运行的是旧版本的JHipster,Swagger ......并且这使问题再次出现,但这次是在Edge上。 删除浏览器的缓存文件可修复Edge和Chrome的错误。 I was using another app to mimic its services and it was probably running older versions of JHipster, Swagger... and that made the problem appear agai ...
-
更新:在swagger-ui的构建1.1.5中可见的swagger模型。 这些模型现在不在UI中显示,但用于swagger代码生成器[ 1 ]。 他们很快就会进入UI,并用于POST,PUT和DELETE等操作,以及更清晰的GET响应可视化方式。 Update: swagger models visible in build 1.1.5 of swagger-ui. The models aren't shown in the UI right now but are used for the swagge ...
-
简单的答案是git clone https://github.com/swagger-api/swagger-ui/tree/master/dist 并将dist内容复制到WAR目录中,然后访问index.html将显示Swagger UI,然后输入通过attachSwaggerSpecificationRestlet定义的/api-docs或/docs attachSwaggerSpecificationRestlet请参阅: 使用针对GAE的Restlet Swagger扩展的指南细节。 The sim ...
-
如何定制swagger-ui?(How to customize swagger-ui?)[2022-03-17]
你究竟想要在Swagger-UI中改变什么? 我自己一直在搞乱品牌/定制Swagger。 如果你看看swagger-ui.js文件,你会发现它有30,000多行代码并且组织不良。 他们也使用我不熟悉的把手和backbone.js。 我通过在呈现页面后简单地操作DOM来添加一些自定义功能。 我是通过编写一个添加到index.html的JS文件来完成的。 I got it done with the following steps : [1] Create : C:\swagger-ui\petstore [ ... -
这些是我采取的步骤: 创建一个新文件SwaggerHeader.css 右键单击SwaggerHeader.css ,选择属性 。 将生成操作设置为嵌入式资源 。 在SwaggerConfig.cs ,添加下面的代码行: .EnableSwaggerUi(“Document / {* assetPath}”,c => {c.InjectStylesheet(typeof(SwaggerConfig).Assembly,ProjectName.FolderName.SwaggerHeader.css“); } ...
-
奇怪但真实。 我最终发现了一些非常罕见的情况。 正如您在屏幕截图中看到的那样,我使用nodeType作为参数,令我惊讶的是,nodeType这个词是html的“document”对象中的保留字。 这就是它造成问题的原因。 我刚刚将nodeType更改为nodeTypeStr,它对我来说很好。 但是,如果Swagger开发人员负责处理保留字,那么它仍然会很好。 Wierd but true. I ended up finding something which is a very rare case. As ...
-
第一张图片是Swagger UI ver。 3 (thentnt版本)。 第二张图片是Swagger UI ver。 2 (以前的版本,不再开发)。 如果由于某种原因想要使用版本2,可以从Swagger UI存储库的2.x分支下载它: https://github.com/swagger-api/swagger-ui/tree/2.x The first image is Swagger UI ver. 3 (the curent version). The second image is Swagger ...
-
正确的电话是 window.swaggerUi.setBasePath('/betabazaar'); 并且需要在onComplete回调中调用。 The correct call is window.swaggerUi.setBasePath('/betabazaar'); and needs to be called in onComplete callback.