如何在openapi驱动的spring boot应用程序中接受json,xml,x-www-form-urlencoded?

问题描述

我正在春季使用gradle插件实现Petstore服务器,以生成API的类: https://github.com/swagger-api/swagger-petstore/blob/swagger-petstore-v3-1.0.5/src/main/resources/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)有效,但适用于xmlx-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来实现接受所有三种媒体类型的请求的方法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)