当前位置: 首页 > 知识库问答 >
问题:

如何获得OpenApi生成器的SpringJava服务器生成响应实体的PUT请求?

公孙宸
2023-03-14

我正在使用openapi生成器maven插件的4.3.1版本在Java11中生成SpringBoot服务器。

对于PUT请求,我希望能够在成功时将URI返回到创建/更新的对象,并且在不成功时返回带有问题信息的纯文本。

我的API的json对于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);
    }

然而,该方法的返回类型是ResponseEntity

我做错了什么吗?或者生成器被硬编码为不允许一个身体响应PUT请求?


共有1个答案

郑松
2023-03-14

生成的代码模板作为资源存储在中。胡子格式。但是,如果你有Maven或Gradle,你可以用相同的名字创建一个修改过的文件,并添加一个指向文件夹的链接,从而轻松地覆盖它。

在您的情况下,将api.mustache和method Body.mustache文件从https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/Java复制到您的计算机或项目资源,并通过替换响应来更改它们

添加项目

 类似资料:
  • 我试图生成一个API客户端从v2 swagger文件openapi生成器cli。为此,我使用openapi生成器cli的docker容器,它将其版本报告为4.1.0-SNAPSHOT。 代码生成使用以下选项: 我还尝试将选项设置为true。 但是,生成的服务类不使用装饰器进行注释。因此,在我的组件中导入它们并在组件的构造函数中添加服务后,我无法使用它们。这就是我的组件的样子: 失败,因为userS

  • 我在openapi v.3规范中有一个模型。我使用openapi生成器maven插件为库webclient(spring 5-webflux)生成java客户端。我想发送回客户端文件和http头。生成的代码没有获取响应标头的方法。 为客户端生成的代码不包含提供对响应头访问的代码。例如,如果我使用库resttemplate,则有一个方法public MultiValueMap getResponse

  • 当使用带有Gradle的OpenAPI生成器时,我希望将性别化的源发送到其他源生成器插件使用的标准目录。类似于Maven生成源的东西。 到目前为止,我还不能这样做,特别是将生成限制为Java源类,而不是整个“原型项目”。 似乎OpenAPI Gradle插件的工作流程与Maven插件的工作流程并不相同。 是否有配置标志来省略所有非java代码的生成,并在“生成的源代码”文件夹(如/out/prod

  • 我使用的是默认配置,但由于某种原因,我一直收到以下错误: 任务“Generate OpenApidocs”执行失败。无法连接到http://localhost:8080/v3/api-文档等待了30秒 似乎由于某种原因,它没有设置服务器。我该怎么修好它?

  • 我在从服务器构建时遇到问题。我的项目是一个使用Netbeans IDE的Android应用程序。当我运行我的应用程序时,一切正常,编译器完全不报告任何错误。但是当我向服务器发送build Netbeans时,我会报告一个成功的构建,但是当我登录到构建服务器时,我看到的是一个构建失败并带有错误日志。 以下是我从生成服务器获得的错误日志: 构建失败/home/ec2 user/android sdk/

  • 我正在使用openapi生成器生成服务器存根python代码。一切正常,但是,每次我修改OpenAPI规范(yaml文件),代码生成器都会覆盖整个代码,甚至是定制的代码(控制器)。我想开发一个增量工作流,如果我对规范进行了更改,生成器将只修改处理该部分代码的代码。 如果我能够执行规范并拥有一个增量工作流,那就太好了。 我使用的是openapi生成器版本3.3。4. 我试图修改控制器并删除,但每次我