-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spring Boot中使用Swagger2构建API文档 #1
Comments
挺不错的. 我们新的项目也准备用这个来管理. |
@2cyhc2 😃 多谢支持 |
如果参数是javabean 如何隐藏javabean中的部分字段 |
@yutaolian 在Swagger注解文档里 有个 |
@Yuicon This application has no explicit mapping for /error, so you are seeing this as a fallback. Thu Nov 30 10:45:37 CST 2017 后台提示: 找不到这个路径,请问是还需要配置什么吗?
} @ApiOperation(value = "新增计划单接口", notes = "新增")
|
@zhuSilence 项目代码 对照着看下有哪里不一样 我没你的项目代码也看不出来为什么 |
@Yuicon 谢谢回复,我已经找到原因了。不过还是感觉哈。
silence
1347023180@qq.com
… 在 2017年11月30日,15:02,Yuicon ***@***.***> 写道:
@zhuSilence <https://github.com/zhusilence> 项目代码 <https://github.com/DigAg/digag-server> 对照着看下有哪里不一样 我没你的项目代码也看不出来为什么
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <#1 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AH1-iRAdQi-flJLuDFjVUmsPPcXSadKBks5s7lMWgaJpZM4Oupfr>.
|
作者你好,我问一下现在我返回的统一格式的响应类,但是我现在想在页面看到实体的属性应该怎么做,如代码:我现在想要知道UserResponse的属性并显示在swaggerui 。但是现在只显示了ApiResult的属性。 |
@851999206 你可以看下这个响应类的实现 |
@Yuicon 谢谢 |
请问一下,这个swagger能通过外网访问么 |
@wanglulei swagger2文档的默认地址是 /swagger-ui.html, 本地开发的访问 http://localhost:8080/swagger-ui.html就可以看到自动生成的文档了. 和自定义的接口没什么区别 |
@Yuicon 我现在是在本地启动一个swagger的项目,然后通过nginx用一个外网ip指向我本地,接口是通的,但是swagger文档却不行。 |
@wanglulei 看看有没有报什么错,我当时也部署到服务器了是没问题的 |
@Yuicon 好的,感谢。 |
接下来我们在Spring Boot中使用Swagger2构建API文档
Swagger是一个简单但功能强大的API表达工具。它具有地球上最大的API工具生态系统,数以千计的开发人员,使用几乎所有的现代编程语言,都在支持和使用Swagger。使用Swagger生成API,我们可以得到交互式文档,自动生成代码的SDK以及API的发现特性等。
我们先来看看具体效果:
可以看到Swagger-Ui是以controller分类,点击一个controller可以看到其中的具体接口,再点击接口就可以看到接口的信息了,如图:
我们可以看到该接口的请求方式,返回数据信息和需要传递的参数.而且以上数据是自动生成的,即使代码有一些修改,Swagger文档也会自动同步修改.非常的方便.
构建RESTful API
在使用Swagger2前我们需要有一个RESTful API的项目. Spring-Boot创建RESTful API项目非常的方便和快速,这里不再介绍如何创建,需要的可以参照项目代码
添加Swagger2依赖
在pom.xml文件中加入以下依赖.
创建Swagger2的Java配置类
通过
@Configuration
注解,表明它是一个配置类,@EnableSwagger2
注解开启swagger2。apiInfo() 方法配置一些基本的信息。createRestApi() 方法指定扫描的包会生成文档,默认是显示所有接口,可以用@ApiIgnore
注解标识该接口不显示。编辑文档接口信息
先看一个例子:
Swagger2提供了一些注解来丰富接口的信息,常用的有:
@ApiOperation
:用在方法上,说明方法的作用@ApiImplicitParams
:用在方法上包含一组参数说明@ApiImplicitParam
:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面@ApiResponses
:用于表示一组响应@ApiResponse
:用在@ApiResponses中,一般用于表达一个错误的响应信息code:状态码
message:返回自定义信息
response:抛出异常的类
访问文档
swagger2文档的默认地址是
/swagger-ui.html
, 本地开发的访问http://localhost:8080/swagger-ui.html
就可以看到自动生成的文档了.完整结果示例可查看项目代码
参考信息
Swagger注解文档
Swagger官方网站
The text was updated successfully, but these errors were encountered: