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

是否可以使用Java中的开放API代码生成器生成返回void但不返回void(对象类型)的控制器?

康赞
2023-03-14

我使用开放式api生成器(gradle的实现)为Java中的api生成控制器,但如果我的endpoint不返回任何内容,则OpenAPI生成器将返回类型生成为对象类型Void,而不是Void。

我希望:

public void createPet() {}

但是得到了:

public Void createPet() { return null; }

我试图自定义胡子模板,但没有任何选项。gradle任务配置中的ConfigOptions也没有相应的选项。

当前。小胡子api模板:

/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) ({{{generatorVersion}}}).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package {{package}};

{{#imports}}import {{import}};
{{/imports}}
{{#swagger2AnnotationLibrary}}
    import io.swagger.v3.oas.annotations.Operation;
    import io.swagger.v3.oas.annotations.Parameter;
    import io.swagger.v3.oas.annotations.Parameters;
    import io.swagger.v3.oas.annotations.media.Content;
    import io.swagger.v3.oas.annotations.media.Schema;
    import io.swagger.v3.oas.annotations.responses.ApiResponse;
    import io.swagger.v3.oas.annotations.security.SecurityRequirement;
    import io.swagger.v3.oas.annotations.tags.Tag;
{{/swagger2AnnotationLibrary}}
{{#swagger1AnnotationLibrary}}
    import io.swagger.annotations.*;
{{/swagger1AnnotationLibrary}}
{{#jdk8-no-delegate}}
    {{#virtualService}}
        import io.virtualan.annotation.ApiVirtual;
        import io.virtualan.annotation.VirtualService;
    {{/virtualService}}
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
{{/jdk8-no-delegate}}
{{#useBeanValidation}}
    import org.springframework.validation.annotation.Validated;
{{/useBeanValidation}}
{{#useSpringController}}
    import org.springframework.stereotype.Controller;
{{/useSpringController}}
import org.springframework.web.bind.annotation.*;
{{#jdk8-no-delegate}}
    {{^reactive}}
        import org.springframework.web.context.request.NativeWebRequest;
    {{/reactive}}
{{/jdk8-no-delegate}}
import org.springframework.web.multipart.MultipartFile;
{{#reactive}}
    import org.springframework.web.server.ServerWebExchange;
    import reactor.core.publisher.Flux;
    import reactor.core.publisher.Mono;
    import org.springframework.http.codec.multipart.Part;
{{/reactive}}

{{#useBeanValidation}}
    import javax.validation.Valid;
    import javax.validation.constraints.*;
{{/useBeanValidation}}
import java.util.List;
import java.util.Map;
{{#jdk8-no-delegate}}
    import java.util.Optional;
{{/jdk8-no-delegate}}
{{^jdk8-no-delegate}}
    {{#useOptional}}
        import java.util.Optional;
    {{/useOptional}}
{{/jdk8-no-delegate}}
{{#async}}
    import java.util.concurrent.{{^jdk8}}Callable{{/jdk8}}{{#jdk8}}CompletableFuture{{/jdk8}};
{{/async}}
import javax.annotation.Generated;

{{>generatedAnnotation}}
{{#useBeanValidation}}
    @Validated
{{/useBeanValidation}}
{{#useSpringController}}
    @Controller
{{/useSpringController}}
{{#swagger2AnnotationLibrary}}
    @Tag(name = "{{{baseName}}}", description = "the {{{baseName}}} API")
{{/swagger2AnnotationLibrary}}
{{#swagger1AnnotationLibrary}}
    @Api(value = "{{{baseName}}}", description = "the {{{baseName}}} API")
{{/swagger1AnnotationLibrary}}
{{#operations}}
    {{#virtualService}}
        @VirtualService
    {{/virtualService}}
    public interface {{classname}} {
    {{#jdk8-default-interface}}
        {{^isDelegate}}
            {{^reactive}}

                default Optional<NativeWebRequest> getRequest() {
                    return Optional.empty();
                    }
            {{/reactive}}
        {{/isDelegate}}
        {{#isDelegate}}

                default {{classname}}Delegate getDelegate() {
                return new {{classname}}Delegate() {};
                }
        {{/isDelegate}}
    {{/jdk8-default-interface}}
    {{#operation}}

            /**
            * {{httpMethod}} {{{path}}}{{#summary}} : {{.}}{{/summary}}
        {{#notes}}
                * {{.}}
        {{/notes}}
            *
        {{#allParams}}
                * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}
        {{/allParams}}
            * @return {{#responses}}{{message}} (status code {{code}}){{^-last}}
                *         or {{/-last}}{{/responses}}
        {{#isDeprecated}}
                * @deprecated
        {{/isDeprecated}}
        {{#externalDocs}}
                * {{description}}
                * @see <a href="{{url}}">{{summary}} Documentation</a>
        {{/externalDocs}}
            */
        {{#virtualService}}
                @ApiVirtual
        {{/virtualService}}
        {{#swagger2AnnotationLibrary}}
                @Operation(
                operationId = "{{{operationId}}}",
            {{#summary}}
                    summary = "{{{.}}}",
            {{/summary}}
            {{#vendorExtensions.x-tags}}
                    tags = { {{#vendorExtensions.x-tags}}"{{tag}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-tags}} },
            {{/vendorExtensions.x-tags}}
                responses = {
            {{#responses}}
                    @ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"{{#baseType}}, content = @Content(mediaType = "application/json", schema = @Schema(implementation =  {{{baseType}}}.class)){{/baseType}}){{^-last}},{{/-last}}
            {{/responses}}
                }{{#hasAuthMethods}},
                security = {
            {{#authMethods}}
                    @SecurityRequirement(name = "{{name}}"{{#isOAuth}}, scopes={ {{#scopes}}"{{scope}}"{{^-last}}, {{/-last}}{{/scopes}} }{{/isOAuth}}){{^-last}},{{/-last}}
            {{/authMethods}}
                }{{/hasAuthMethods}}
                )
        {{/swagger2AnnotationLibrary}}
        {{#swagger1AnnotationLibrary}}
                @ApiOperation(
                tags = { {{#vendorExtensions.x-tags}}"{{tag}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-tags}} },
                value = "{{{summary}}}",
                nickname = "{{{operationId}}}",
                notes = "{{{notes}}}"{{#returnBaseType}},
                response = {{{.}}}.class{{/returnBaseType}}{{#returnContainer}},
                responseContainer = "{{{.}}}"{{/returnContainer}}{{#hasAuthMethods}},
                authorizations = {
            {{#authMethods}}
                {{#isOAuth}}
                        @Authorization(value = "{{name}}", scopes = {
                    {{#scopes}}
                            @AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{^-last}},{{/-last}}
                    {{/scopes}}
                        }){{^-last}},{{/-last}}
                {{/isOAuth}}
                {{^isOAuth}}
                        @Authorization(value = "{{name}}"){{^-last}},{{/-last}}
                {{/isOAuth}}
            {{/authMethods}} }{{/hasAuthMethods}}
                )
                @ApiResponses({
            {{#responses}}
                    @ApiResponse(code = {{{code}}}, message = "{{{message}}}"{{#baseType}}, response = {{{.}}}.class{{/baseType}}{{#containerType}}, responseContainer = "{{{.}}}"{{/containerType}}){{^-last}},{{/-last}}
            {{/responses}}
                })
        {{/swagger1AnnotationLibrary}}
        {{#implicitHeaders}}
            {{#swagger2AnnotationLibrary}}
                    @Parameters({
                {{#headerParams}}
                    {{>paramDoc}}{{^-last}},{{/-last}}
                {{/headerParams}}
            {{/swagger2AnnotationLibrary}}
            {{#swagger1AnnotationLibrary}}
                    @ApiImplicitParams({
                {{#headerParams}}
                    {{>implicitHeader}}{{^-last}},{{/-last}}
                {{/headerParams}}
            {{/swagger1AnnotationLibrary}}
                })
        {{/implicitHeaders}}
            @RequestMapping(
            method = RequestMethod.{{httpMethod}},
            value = "{{{path}}}"{{#singleContentTypes}}{{#hasProduces}},
            produces = "{{{vendorExtensions.x-accepts}}}"{{/hasProduces}}{{#hasConsumes}},
            consumes = "{{{vendorExtensions.x-contentType}}}"{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}},
            produces = { {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }{{/hasProduces}}{{#hasConsumes}},
            consumes = { {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }{{/hasConsumes}}{{/singleContentTypes}}
            )
        {{#jdk8-default-interface}}default {{/jdk8-default-interface}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}{{>returnTypes}}{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}(
        {{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}},
        {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}},
        {{/hasParams}}{{#swagger2AnnotationLibrary}}@Parameter(hidden = true){{/swagger2AnnotationLibrary}}{{#springFoxDocumentationProvider}}@ApiIgnore{{/springFoxDocumentationProvider}} final ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}{{#hasParams}},
        {{/hasParams}}{{#springFoxDocumentationProvider}}@ApiIgnore {{/springFoxDocumentationProvider}}{{#springDocDocumentationProvider}}@ParameterObject {{/springDocDocumentationProvider}}final Pageable pageable{{/vendorExtensions.x-spring-paginated}}
            ){{^jdk8-default-interface}};{{/jdk8-default-interface}}{{#jdk8-default-interface}}{{#unhandledException}} throws Exception{{/unhandledException}} {
        {{#delegate-method}}
                return {{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, pageable{{/vendorExtensions.x-spring-paginated}});
                }

                // Override this method
            {{#jdk8-default-interface}}default {{/jdk8-default-interface}} {{#responseWrapper}}{{.}}<{{/responseWrapper}}{{>returnTypes}}{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}}{{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{{dataType}}}{{/reactive}}{{#reactive}}{{^isArray}}Mono<{{{dataType}}}>{{/isArray}}{{#isArray}}Flux<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{/isFile}}{{#isFile}}{{#reactive}}Flux<Part>{{/reactive}}{{^reactive}}MultipartFile{{/reactive}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}{{#springFoxDocumentationProvider}}@ApiIgnore{{/springFoxDocumentationProvider}} final ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, {{#springFoxDocumentationProvider}}@ApiIgnore{{/springFoxDocumentationProvider}} final Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{#unhandledException}} throws Exception{{/unhandledException}} {
        {{/delegate-method}}
        {{^isDelegate}}
            {{>methodBody}}
        {{/isDelegate}}
        {{#isDelegate}}
                return getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, pageable{{/vendorExtensions.x-spring-paginated}});
        {{/isDelegate}}
            }{{/jdk8-default-interface}}

    {{/operation}}
        }
{{/operations}}

在gradle.build中生成任务示例

openApiGenerate {
    generatorName = "spring"
    inputSpec = "$projectDir/src/main/resources/swagger/api.yaml".toString()
    outputDir = "$buildDir/generated".toString()
    apiPackage = "org.example.api"
    modelPackage = "org.example.resource"
    templateDir = "$projectDir/src/main/resources/templates/".toString()

    configOptions = [
        java8: "false",
        serializableModel: "true",
        interfaceOnly: "true",
    ]
}


应用程序编程接口。亚马尔

swagger: "2.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
host: petstore.swagger.io
basePath: /v1
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          type: integer
          format: int32
      responses:
        "200":
          description: A paged array of pets
          headers:
            x-next:
              type: string
              description: A link to the next page of responses
          schema:
            $ref: '#/definitions/Pets'
        default:
          description: unexpected error
          schema:
            $ref: '#/definitions/Error'
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        200:
          description: Ok
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          type: string
      responses:
        "200":
          description: Expected response to a valid request
          schema:
            $ref: '#/definitions/Pets'
        default:
          description: unexpected error
          schema:
            $ref: '#/definitions/Error'
definitions:
  Pet:
    type: "object"
    required:
      - id
      - name
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      tag:
        type: string
  Pets:
    type: array
    items:
      $ref: '#/definitions/Pet'
  Error:
    type: "object"
    required:
      - code
      - message
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string

共有2个答案

孔运良
2023-03-14

我找到了解决方法。我使用了“查找并替换任务”来表示它。

  1. 使用openApiGenerate任务生成API

如何查找和替换:

Gradle任务替换中的字符串。java文件

孙梓
2023-03-14

让我们把openapi生成器的版本看作当前版本。

强制生成器使用void返回类型而不是void返回类型似乎不可行。

作为最后的手段,可以考虑创建生成器的分支并实现所需的行为。

GitHub问题已经要求该功能:更改Spring openapi generator maven插件生成接口的返回类型·问题#6135·openapi工具/openapi生成器。

相关问题:java-更改Spring openapi生成器maven插件生成接口的返回类型-堆栈溢出。

spring生成器(generatorName:spring)由组织表示。openapitools。codegen。语言。SpringCodegen类。

Void类型检测由SpringCodesen类实现。

请参阅源代码的相关部分:openapi-生成器/SpringCodegen.java在v5.3.1·OpenAPITools/openapi-生成器:

private void doDataTypeAssignment(String returnType, DataTypeAssigner dataTypeAssigner) {
    final String rt = returnType;
    if (rt == null) {
        dataTypeAssigner.setReturnType("Void");
    } else if (rt.startsWith("List")) {

 类似资料:
  • 在给定以下约束的情况下,是否可以以某种方式编译: 您不能向添加任何其他修饰符。 不包含或,您不能更改它。 无法将重写器添加到编译器管道。 您可以更改周围的其他所有内容。

  • 是否有方法重写build方法返回SampleClass以避免执行build.build?

  • 我有下面的课,我试图测试。我遇到问题的方法是,因为我试图存根/模拟行为,然后在测试中验证行为。

  • 问题内容: 我看到的唯一的void返回类型在方法中具有System.out.println语句。因此,一旦调用该方法,这些字符串就会被打印出来。 您是否可以使返回类型为字符串并让该字符串返回而不是执行void返回类型? 如果void返回类型方法中包含其他方法,您能否将返回类型设为该方法给出的值,该值将返回该方法的结果? 什么时候只能使用void返回类型? 问题答案: 有人可以解释Java中的voi

  • 问题内容: 如果不是原始类型,返回类型的正确方法是什么?例如。我目前使用null如下。 问题答案: Void类是一个无法实例化的占位符类,用于保存对表示Java关键字void的Class对象的引用。 因此,以下任何条件就足够了: 参数化并返回或 参数化并返回 用您的一个参数化 你不能让这种方法,和其他任何回报 的东西 。由于忽略了某些内容,因此您可以返回任何内容。

  • 生成器返回值 PHP7支持通过Generator::getReturn获取生成器方法return的返回值。 PHP5中我们约定使用Generator最后一次yield值作为返回值。 <?php final class AsyncTask { public function begin() { return $this->next(); } //