Swagger bug-当 http 请求方法是 get 时 Operation.getConsumes() 不应该返回 application/json

问题由来

相关的 issue 可见: https://github.com/springfox/springfox/issues/2815

最近在用 swagger lib 的时候发现一个问题, 就是当 HTTP 请求方法是 GET 时候, 返回的 consumeapplication/json, 感觉有些不对劲, 于是乎就做了如下的实验:

比如有如下一个方法:

1
2
3
4
5
6
7
8
9
@Controller
@Api(value ="SwaggerTestService", tags = "SwaggerTest")
public class SwaggerTestController {
@RequestMapping(value= "/SwaggerTest/getMethodTest",method = RequestMethod.GET)
@ApiOperation(value="(SwaggerTestService) getMethodTest", notes="(SwaggerTestService) getMethodTest")
public BaseResponse getMethodTest() {
return null;
}
}

当访问 /v2/api-docs 的时候返回的信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
"/SwaggerTest/getMethodTest": {
"get": {
"tags": [
"SwaggerTest"
],
"summary": "(SwaggerTestService) getMethodTest",
"description": "(SwaggerTestService) getMethodTest",
"consumes": [
"application/json"
],
"produces": [
"*/*"
]
}

可以发现这里 consumes 返回的是 application/json, 稍微思考下, 会发现其实是不太对的.其实这就意味着 GET 请求的 Content-Typeapplication/json, 但是 HTTP GET 请求怎么会是 application/json 呢? 显然是不对的.相关的 GET 请求是否需要 Content-Type 讨论可见: https://stackoverflow.com/questions/5661596/do-i-need-a-content-type-for-http-get-requests, 从中我们可以看出, 只有 POSTPUT 需要请求的 Content-Type.

因为是使用 swagger lib 相关 API 的时候发现的问题, 所以进一步发现是因为 io.swagger.models.Operation#getConsumes() 的返回值有问题.所以在我们使用此 API 的时候, 需要注意, 如果是 GET 请求, 那么是可以忽略此方法的返回值的.

xwiki 文档迁移到 confluence-(2) 如何将上传的 pdf 显示在 confluence 页面中
xwiki 文档迁移到 confluence-(0) 概述