最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Swagger 'GET' request always returns as texthtml Accept type on response and NOT applicationjson -

programmeradmin1浏览0评论

I have a swagger tag document using the Swagger UI that always returns text/html but it should return application/json. The POST requests and every other type returns application/json but this particular GET request does not. The service end point code is correct. And if I change the request to POST it does return as application/json. So it is just type GET within swagger which does not return the correct type. Any thoughts how to correct the call within the UI to use the application/json?

This is swagger version 2.1.4 that was downloaded recently from the swagger site.

"/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

Referer:http://localhost:9086/swagger/index.html

Here is the Java code Spring Restful definition:

@RequestMapping(value="bankName",
    method=RequestMethod.GET,
    produces=MediaType.APPLICATION_JSON_VALUE)

I have a swagger tag document using the Swagger UI that always returns text/html but it should return application/json. The POST requests and every other type returns application/json but this particular GET request does not. The service end point code is correct. And if I change the request to POST it does return as application/json. So it is just type GET within swagger which does not return the correct type. Any thoughts how to correct the call within the UI to use the application/json?

This is swagger version 2.1.4 that was downloaded recently from the swagger site.

"/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

Referer:http://localhost:9086/swagger/index.html

Here is the Java code Spring Restful definition:

@RequestMapping(value="bankName",
    method=RequestMethod.GET,
    produces=MediaType.APPLICATION_JSON_VALUE)
Share Improve this question edited Apr 27, 2016 at 8:44 ADreNaLiNe-DJ 4,9194 gold badges27 silver badges35 bronze badges asked Apr 6, 2016 at 13:04 Berlin BrownBerlin Brown 11.8k38 gold badges140 silver badges213 bronze badges 12
  • 3 why your get has "consumes"? – Junbang Huang Commented Apr 18, 2016 at 3:17
  • 2 GET requests do not have content-type as they do not have body. – Tamas Hegedus Commented Apr 23, 2016 at 13:56
  • 1 I guess there is problem in your API response. Can you check if your GET API response content-type is application/json? – emil Commented Apr 26, 2016 at 1:01
  • 4 Please elaborate how a request in the Swagger UI can "return" anything and what you mean by "correcting the call within the UI". Since the Swagger UI will only display the spec nicely and allow to send requests accordingly, but doesn't control what is returned by the server, it doesn't make sense to me at the moment. Is the problem that the server returns the wrong content type or Swagger UI just displays it wrong? If you, for example, manually use the CURL mand the UI creates in a shell (add -i param for headers), what is returned then? Can you copy the CURL mand and its result here? – CherryDT Commented Apr 26, 2016 at 10:55
  • 2 Swagger UI is just a tool for designing and documenting an API. It seems like the problem may be in the framework/code where your API is implemented. Do you have two functions where one handles the POST request and a second handles the GET request? – drsnark Commented Aug 4, 2017 at 19:27
 |  Show 7 more ments

1 Answer 1

Reset to default 1

Have you tried this?

"/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"
            }                       
        }
    }
}  

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论