18
votes

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)
1
why your get has "consumes"?Junbang Huang
GET requests do not have content-type as they do not have body.Tamas Hegedus
I guess there is problem in your API response. Can you check if your GET API response content-type is application/json?emil
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 command the UI creates in a shell (add -i param for headers), what is returned then? Can you copy the CURL command and its result here?CherryDT
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

1 Answers

1
votes

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