Reputation: 513
I have a Spring Boot REST app that is Swagger-enabled using springdoc-openapi-ui. For the life of me, I can't get the generated Swagger UI to actually POST JSON. Instead, the frontend passes args as request parameters. How do I get the Swagger UI to actually submit JSON?
@Data
public static class SampleData {
private String info;
}
@PostMapping(value = "/test", consumes = "application/json")
public void postData(@ParameterObject @RequestBody SampleData data) {
// do something
}
After clicking "Try it out" and "Execute" in the Swagger UI, the request fails with:
{
"status": 415,
"error": "Unsupported Media Type",
...
}
Because the UI tried to POST this:
http://localhost:8080/myapp/v1/test?info=test
The generated curl command is also incorrect (not POSTing JSON):
curl -X 'POST' \
'http://localhost:8080/myapp/v1/test?info=test' \
-H 'accept: */*' \
-d ''
Note that the Swagger UI will POST JSON if I remove the @ParameterObject
annotation. However, then the UI doesn't present the user-friendly form (which in the full example contains documentation annotations for fields in that bean)
What am I missing?
Spring Boot: 2.6.3, springdoc-openapi-ui: 1.6.5
Upvotes: 3
Views: 1512
Reputation: 513
Resolution from springdoc
github was to remove the @ParameterObject annotation and switch to the "Schema" view in the UI to see the documentation for the POJO fields. Not quite the solution I was hoping for but it works. github.com/springdoc/springdoc-openapi/issues/1482
Upvotes: 1