我有一个使用springdoc-openapi-ui启用Swagger的Spring Boot REST应用程序。但是,我无论如何都无法使生成的Swagger UI实际提交JSON,而是前端将参数作为请求参数传递。我该如何使Swagger UI实际提交JSON?
@Data
public static class SampleData {
private String info;
}
@PostMapping(value = "/test", consumes = "application/json")
public void postData(@ParameterObject @RequestBody SampleData data) {
// do something
}
在Swagger UI中点击“试一试”和“执行”后,请求失败并显示以下错误信息:
{
"status": 415,
"error": "Unsupported Media Type",
...
}
因为用户界面试图提交以下内容:
http://localhost:8080/myapp/v1/test?info=test
生成的curl命令也不正确(未以POST方式提交JSON数据):
curl -X 'POST' \
'http://localhost:8080/myapp/v1/test?info=test' \
-H 'accept: */*' \
-d ''
请注意,如果我移除
@ParameterObject
注解,Swagger UI会使用POST方式提交JSON数据。然而这样做会导致UI无法呈现用户友好的表单(在完整示例中,这个bean字段有文档注解)。我漏掉了什么?
Spring Boot版本为2.6.3,springdoc-openapi-ui版本为1.6.5。
springdoc
的 Github 上开一个问题:https://github.com/springdoc/springdoc-openapi/issues。 - João Dias@ParameterObject
注释,并切换到UI中的“Schema”视图以查看POJO字段的文档。虽然不完全是我希望的解决方案,但它可以工作。 https://github.com/springdoc/springdoc-openapi/issues/1482 - Noky