知识点
相关文章
更多最近更新
更多springboot整合Swagger2
2020-08-23 09:26|来源: 656463
Swagger2是一个开源项目,用于为RESTful Web服务自动生成REST API文档。 它提供了一个用户界面,可通过浏览器访问访问和调试API接口。
1、配置maven依赖
<!-- swagger2 配置 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
在github上有扩展的swagger-ui插件,地址:https://github.com/xiaoymin/swagger-bootstrap-ui,如果想使用这个依赖,可以加入以下依赖即可
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.6</version> </dependency>
对于使用Gradle构建的项目,在build.gradle 中加入
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.4.0' compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.4.0'
2、在Spring Boot应用程序中配置Swagger2
创建swagger2的配置启动类,配置swagger2核心配置 docket和ApiInfo两个bean
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2 { // @Bean public Docket createRestApi() { //指定api类型为swagger2 return new Docket(DocumentationType.SWAGGER_2) //配置项目api文档的描述 .apiInfo(apiInfo()) .select() //需要生产api文档的包路径 .apis(RequestHandlerSelectors.basePackage("xx.controller")) .paths(PathSelectors.any()) // 所有controller .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() //文档页标题 .title("文档页标题") //联系人信息 .contact(new Contact("领悟书生","https://www.656463.com")) .description("详细信息") // 文档版本号 .version("1.0.1") // 网站地址 .termsOfServiceUrl("https://www.656463.com") .build(); } }
3、配置Swagger2 api
3.1、配置整个的API描述
@Api(value = "用户操作", tags = {"操作用户的相关接口"})
3.2、忽略某个接口类
@ApiIgnore
3.3、接口api配置
使用@ApiOperation配置接口配置
使用@ApiParam配置参数描述
@ApiOperation(value = "根据用户Id获取用户信息", notes = "根据用户Id获取用户信息", httpMethod = "GET") @PostMapping("getUser") public void getUser( @ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId) { ...... } }
如果参数是复杂对象,使用@ApiModel注释对象,@ApiModelProperty注释属性,如
@ApiModel(value = "用户对象", description = "用户对象") public class User { @ApiModelProperty(value = "用户名", name = "username", example = "张三", required = true) private String username; @ApiModelProperty(value = "密码", name = "password", example = "123456", required = true) private String password; //set get }
相关问答
更多-
springboot 和springmvc怎么整合[2022-08-22]
在SpringBoot中添加依赖如下: org.springframework.boot spring-boot-starter-web 即可 -
如何整合SpringMVC和Swagger2,并且使用Mock数据进行联调[2022-03-18]
在开发中,我们经常会需要对接口进行联调沟通,然而这是在整个开发周期中最占用时间的一块。尤其最近在接手一个项目中,采用前后端分离开发的模式,写前端的同学完全不懂后台代码(对,我们之前都是一个人写前后端所有相关的代码滴!),接口经常性需要进行调整,基本就是重复沟通,重复浪费时间。于是我决定在项目中引进神器“Swagger”,用来一键生成文档,减少了后端开发同学编写接口文档的时间,同时前端采用Mock数据的方式,省略了等待接口的时间。 -
Spring Boot进阶1 - 整合Swagger2自动生成RESTful API文档[2022-03-02]
首先,我们需要定义一个API项目然后通过Nuget引入组件。记住选下图中的第三个。引入成功后,将向项目里面添加一些主要文件:•Scripts\WebApiTestClient.js•Areas\HelpPage\TestClient.css•Areas\HelpPage\Views\Help\DisplayTemplates\TestClientDialogs.cshtml•Areas\HelpPage\Views\Help\DisplayTemplates\TestClientReferences.csh ... -
swagger 怎么整合springmvc 文档[2022-04-22]
groupId>io.springfoxspringfox-swagger2 2.5.0 com.fasterxml.jackson.core jackson-annotations org.sl ... -
SpringBoot整合redis后,是否自带了连接池管理[2022-01-28]
是的你只需要在你的配置文件稍微配置一下就能用,把我们项目的redis配置给你参考一下 redis: host: port: password: # 连接超时时间(毫秒) timeout: 10000 pool: # 连接池中的最大空闲连接 max-idle: 100 # 连接池中的最小空闲连接 min-idle: 10 # 连接池最大连接数(使用负值表示没有限制) max-active: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: -1 # # 连接池中的最大空闲连接 # ... -
为什么springboot中swagger2没有使用任何注解最后也能生成接口文档[2023-10-25]
Swagger-UI纯碎的基于html+javascript实现,在.NET中应用,也就是在 asp.net 页面中应用相应的 js,按照规范写 html 就可以了。 有一个Swagger.Net.UI 你可以搜索一下。 -
/v2/api-docs URL是SpringFox用于文档的默认值。 v2没有引用您的API文档版本(可以在Docket配置中进行更改),而是使用了Swagger规范的版本。 看看这里的文档来自定义Swagger文档URL。 简而言之,您需要修改环境属性以更改文档将显示在的URL: springfox.documentation.swagger.v2.path=/my/docs 这会将SpringFox Swagger文档的默认URL从/v2/api-docs更改为您指定的任何内容。 要实现这一点,请将 ...
-
当我尝试使用url> http:// localhost:8080 / greetingservice / swagger-ui.html访问类似于文档中描述的文档时,我收到404错误 您需要按如下方式设置应用程序上下文路径: 在src/main/resources创建application.properties并添加以下行: server.context-path=/greetingservice 参考: http://docs.spring.io/spring-boot/docs/current/re ...
-
Spring Swagger2集成ServletContext自动装配问题(Spring Swagger2 integration ServletContext autowiring issue)[2022-07-05]
这是你的问题: return new DocumentationPluginsBootstrapper(documentationPluginsManager, handlerProviders, scanned, resourceListing, typeResolver, defaults, new ServletContextFactory().getObject()); 当你调用new来创建ServletContextFactory 。 将依赖项注入 ... -
目前在Spring REST Docs中没有开箱即用的支持。 您打开的问题将跟踪添加此类功能的可能性。 与此同时,您最好的选择是编写一个自定义Snippet实现,生成(部分)Swagger规范。 通常,Spring REST Docs代码段处理记录单个资源,而Swagger规范描述整个服务。 这意味着Swagger规范Snippet实现需要以某种方式累积状态,然后在最后生成完整的规范。 有很多方法可以做到这一点(在内存中,在后处理步骤中组合的多个文件等)。 我不清楚一种方法显然是正确的方法,因此一些实验将是 ...