我有一个使用 Swagger UI 的 swagger 标签文档,它总是返回 text/html 但它应该返回 application/json。 POST 请求和所有其他类型都返回 application/json 但这个特定的 GET 请求不会。服务端点代码正确。如果我将请求更改为 POST,它会作为 application/json 返回。所以它只是在 swagger 中键入 GET,它不会返回正确的类型。有没有想过如何更正 UI 中的调用以使用 application/json?
这是最近从 swagger 站点下载的 swagger 版本 2.1.4。
"/bankName": {
"get": {
"summary": "Bank Name Search",
"description": "Bank Name Search, input routing number to return bank name",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "query",
"name": "routingNumber",
"description": "Input Bank Routing Number",
"required": true,
"type": "string",
}
],
"responses": {
"200": {
"description": "An array",
"schema": {
"type": "object",
"properties": {
"errorInfo": {
"$ref": "#/definitions/ErrorInfo"
},
"bankName": {
"type": "string",
}
}
}
},
"400": {
"description": "Invalid Request Input supplied"
},
"500": {
"description": "General Unexpected Error"
}
}
}
}
Accept:application/json
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:localhost:9086
Origin:http://localhost:9086
Pragma:no-cache
下面是 Java 代码 Spring Restful 定义:
@RequestMapping(value="bankName",
method=RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE)
最佳答案
你试过吗?
"/bankName": {
"get": {
"summary": "Bank Name Search",
"description": "Bank Name Search, input routing number to return bank name",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "query",
"name": "routingNumber",
"description": "Input Bank Routing Number",
"required": true,
"type": "string",
}
],
"responses": {
"200": {
"description": "An array",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"errorInfo": {
"$ref": "#/definitions/ErrorInfo"
},
"bankName": {
"type": "string",
}
}
}
}
}
},
"400": {
"description": "Invalid Request Input supplied"
},
"500": {
"description": "General Unexpected Error"
}
}
}
}
关于javascript - Swagger 'GET' 请求总是以 text/html 形式返回 Accept type on response 而不是 application/json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36452056/