2

I've created a Spring Boot application with RestController. I enabled Swagger UI and it works fine as I can login to the UI and execute any GET methods. But for POST methods accepting objects in the body, when I fire off the request using Swagger UI, it always returns 400 status code. I can see the request never reached the particular POST method. May I know if any special config I need for Swagger UI?

The particular method in my Spring Boot RestController

@ApiOperation(value = "Query requests by search criteria")
@RequestMapping(value = "/api/query", method = POST)
public PageResult<MyPOJO> find(@RequestBody SearchRequest request) {
    //implementation omitted.
}

and I'm using

 "io.springfox:springfox-swagger2:2.1.2",
 "io.springfox:springfox-swagger-ui:2.1.2"

This one (generated by Swagger UI) for the corresponding api gives me 400 bad request:

 {
  "listEntrySearchCriteria": {
    "summary": {
      "createdBy": "string",
      "createdOn": "2016-03-21T10:33:05.048Z",
      "effectiveEnd": "2016-03-21T10:33:05.048Z",
      "effectiveStart": "2016-03-21T10:33:05.048Z",
      "id": 0,
      "region": "string",
      "type": "ETB",
      "updatedBy": "string",
      "updatedOn": "2016-03-21T10:33:05.048Z",
      "version": 0
    }
  },
  "listSummarySearchCriteria": {
    "effectiveEnd": "2016-03-21T10:33:05.048Z",
    "effectiveStart": "2016-03-21T10:33:05.048Z",
    "statuses": [
      "string"
    ],
    "types": [
      "string"
    ]
  },
  "pageRequest": {
    "orders": [
      {
        "direction": "ASC",
        "ignoreCase": true,
        "nullHandling": "NATIVE",
        "property": "string"
      }
    ],
    "page": 0,
    "size": 0
  }
}

But if I supply a random request for the same method, it at least reaches my method:

  {
  "orders": [
    {
      "direction": "ASC",
      "ignoreCase": true,
      "nullHandling": "NATIVE",
      "property": "id"
    }
  ],
  "page": 0,
  "size": 0
}
enfany
  • 875
  • 2
  • 15
  • 33
  • Before we know any special config is necessary we need your config/code. At least the affected code in your RestController and the code you used to enable Swagger UI. Without that it will be hard to make any conclusion. – g00glen00b Mar 21 '16 at 08:56
  • @g00glen00b I've updated my post with more details. – enfany Mar 21 '16 at 09:26
  • According to what I see it should work fine. Did you test it out using a REST client (like Postman) already? If it works using a REST client, can you make sure that the path is the same as the one used in Swagger? I'm starting to think it may not have anything to do with Springfox. – g00glen00b Mar 21 '16 at 09:46
  • I realized if I use the request generated by Swagger UI for this method, I got 400 response code, if I supply some random request body, it at least reaches my method. The difference between two requests are the one generated by Swagger UI is much longer than the one I supplied. Does request content length matter? – enfany Mar 21 '16 at 10:39
  • How long is "much longer"? By default the max size of your post body can be 2MB if I'm not mistaken (it's a Tomcat default). – g00glen00b Mar 21 '16 at 11:01
  • it must be less than 2MB. I'm using Spring Boot Jetty – enfany Mar 21 '16 at 11:04
  • For Jetty it appears to be less indeed, about 195KB. But if the provided JSON is the content, then it should still work, even for Jetty. Anyways, if you test your REST API (without Swagger) and you provide the generated JSON, does it still work? If it doesn't, then the problem isn't related to Swagger or Springfox. Is there no error/message in your log or in the response body? – g00glen00b Mar 21 '16 at 11:13
  • @enfany Id recommend first creating a json payload that works. Then take that and compare it with what springfox *infers* from the objects. That should give you some clues as to what might be wrong – Dilip Krishnan Apr 07 '16 at 16:08

0 Answers0