如何获取 Spring Java 服务器的 OpenApi 生成器为 PUT 请求生成 ResponseEntity<Object>?

How to get OpenApi Generator for Spring Java server generate ResponseEntity<Object> for PUT request?

我在Java11.

中使用4.3.1版本的openapi-generator-maven-plugin生成SpringBoot服务器

对于 PUT 请求,我希望能够在成功时 return created/updated 对象的 URI,以及在不成功时包含有关问题信息的纯文本。

我的 json API 具有以下 PUT 请求内容:

"put": {
        "summary": "Create or update a Service",
        "deprecated": false,
        "operationId": "putIndividualServiceUsingPUT",
        "responses": {
          "200": {
            "description": "Service updated"
          },
          "201": {
            "description": "Service created",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string",
                  "example": "services/DroneIdentifier"
                }
              }
            }
          },
          "400": {
            "description": "Provided service is not correct",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string",
                  "example": "Service is missing required property version"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized"
          },
          "403": {
            "description": "Forbidden"
          }
        },
        "parameters": [
          {
            "name": "serviceName",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "example": "DroneIdentifier"
          }
        ],
        "requestBody": {
          "description": "Service to create/update",
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/service"
              }
            }
          }
        }

生成的API:

    /**
     * PUT /services/{serviceName} : Create or update a Service
     *
     * @param serviceName  (required)
     * @param service Service to create/update (required)
     * @return Service updated (status code 200)
     *         or Service created (status code 201)
     *         or Provided service is not correct (status code 400)
     *         or Unauthorized (status code 401)
     *         or Forbidden (status code 403)
     */
    @ApiOperation(value = "Create or update a Service", nickname = "putIndividualServiceUsingPUT", notes = "", tags={ "rAPP Catalogue API", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "Service updated"),
        @ApiResponse(code = 201, message = "Service created", response = String.class),
        @ApiResponse(code = 400, message = "Provided service is not correct", response = String.class),
        @ApiResponse(code = 401, message = "Unauthorized"),
        @ApiResponse(code = 403, message = "Forbidden") })
    @RequestMapping(value = "/services/{serviceName}",
        produces = { "text/plain" }, 
        consumes = { "application/json" },
        method = RequestMethod.PUT)
    default ResponseEntity<Void> putIndividualServiceUsingPUT(@ApiParam(value = "",required=true) @PathVariable("serviceName") String serviceName,@ApiParam(value = "Service to create/update" ,required=true )  @Valid @RequestBody Service service) {
        return getDelegate().putIndividualServiceUsingPUT(serviceName, service);
    }

然而,方法的 return 类型是 ResponseEntity<Void>,这意味着我无法在响应正文中放入任何内容。

我是不是做错了什么?或者生成器是否被硬编码为不允许在 PUT 请求的响应中使用正文?

生成的代码模板以 .mustache 格式存储为资源。但是如果您有 Maven 或 Gradle.

,您可以通过创建具有相同名称的修改文件并向文件夹添加 link 轻松覆盖它

在您的情况下,将 api.mustache 和 methodBody.mustache 文件从 https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/Java 复制到您的计算机或项目资源,并通过将 Response<> 替换为 URI.

添加项目 <templateResourcePath>folderInYourResources</templateResourcePath><templateDirectory>pathToDirectory</templateDirectory> 进入你的 Maven <configuration> 一切都应该工作