我在春天使用 gradle 插件为 API 生成类来实现一个 petstore 服务器: https ://github.com/swagger-api/swagger-petstore/blob/swagger-petstore-v3-1.0.5/src/主/资源/openapi.yaml
这种生成的 API 代码的示例是
...
public interface UserApi {
...
@PostMapping(
value = "/user",
produces = { "application/json", "application/xml" },
consumes = { "application/json", "application/xml", "application/x-www-form-urlencoded" }
)
default ResponseEntity<User> createUser(@ApiParam(value = "Created user object" ) @Valid @RequestBody(required = false) User user) {
....
对于一个最小的例子,我什至不覆盖上述方法。
使用 springdoc,我可以在 http://localhost:8080/swagger-ui.html 创建一个从代码生成的 swagger ui 页面。在那里,我可以为 api 提供的所有媒体类型生成 curl 请求:
curl -X POST "http://localhost:8080/user" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"id\":0,\"username\":\"string\",\"firstName\":\"string\",\"lastName\":\"string\",\"email\":\"string\",\"password\":\"string\",\"phone\":\"string\",\"userStatus\":0}"
curl -X POST "http://localhost:8080/user" -H "accept: application/json" -H "Content-Type: application/xml" -d "<?xml version=\"1.0\" encoding=\"UTF-8\"?><User>\t<id>0</id>\t<username>string</username>\t<firstName>string</firstName>\t<lastName>string</lastName>\t<email>string</email>\t<password>string</password>\t<phone>string</phone>\t<userStatus>0</userStatus></User>"
curl -X POST "http://localhost:8080/user" -H "accept: application/json" -H "Content-Type: application/x-www-form-urlencoded" -d "id=0&username=string&firstName=string&lastName=string&email=string&password=string&phone=string&userStatus=0"
第一个(json)有效,但xml
我x-www-form-urlencoded
得到:
{"timestamp":"2020-10-19T22:42:43.215+00:00","status":415,"error":"Unsupported Media Type","message":"","path":"/user"}
该响应代码不在生成的默认实现中,它必须在之前的步骤中抛出。
stackoverflow 上已经有类似的问题,但是通过 openapi 我不能只省略注释。有没有办法让我使用 openapi 和 spring boot 来实现接受所有三种媒体类型的请求?