java OpenApi Generator可以生成接口伪客户端吗?

agxfikkp  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(211)

是否可以使用OpenApiGenerator生成虚拟客户端?
这是我目前的设置:

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.11'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
    id "org.openapi.generator" version "6.3.0"
}

group = 'dev.rifaii'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.0.2'

    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

def swaggerCodeGenConfig = {
    generatorName = "spring"
    remoteInputSpec = "someUrl"
    outputDir = "$projectDir/external/src/main/java"
    apiPackage = 'somepackageForApis'
    modelPackage = 'somepackageForModels'
    additionalProperties = [
            packageName: "petstore",
            interfaceOnly: 'true',
            feign      : 'true'
    ]
}

task buildExampleClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    configure swaggerCodeGenConfig
    remoteInputSpec = "http://158.101.191.70:8081/v3/api-docs"
    apiPackage = 'dev.rifaii.example.api'
    modelPackage = 'dev.rifaii.example.model'
}

此任务当前正在生成的内容:

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2023-05-09T00:05:09.505990300+03:00[Europe/Tallinn]")
@Validated
@Tag(name = "store", description = "the store API")
public interface StoreApi {

    default Optional<NativeWebRequest> getRequest() {
        return Optional.empty();
    }

    /**
     * DELETE /store/order/{orderId} : Delete purchase order by ID
     * For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
     *
     * @param orderId ID of the order that needs to be deleted (required)
     * @return Invalid ID supplied (status code 400)
     *         or Order not found (status code 404)
     */
    @Operation(
        operationId = "deleteOrder",
        summary = "Delete purchase order by ID",
        description = "For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors",
        tags = { "store" },
        responses = {
            @ApiResponse(responseCode = "400", description = "Invalid ID supplied", content = {
                @Content(mediaType = "*/*", schema = @Schema(implementation = Map.class))
            }),
            @ApiResponse(responseCode = "404", description = "Order not found")
        }
    )
    @RequestMapping(
        method = RequestMethod.DELETE,
        value = "/store/order/{orderId}",
        produces = { "*/*" }
    )
    default ResponseEntity<Void> deleteOrder(
        @Parameter(name = "orderId", description = "ID of the order that needs to be deleted", required = true, in = ParameterIn.PATH) @PathVariable("orderId") Long orderId
    ) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }
   }

我想要的就是它能生成像这样的接口

@FeignClient("someClient")
public interface SomeClient {
    @RequestMapping(method = RequestMethod.GET, value = "/example")
    List<SomeObject> getExample();
}

在另一个项目中,我使用了Springfox的旧版本的swaggercodegen,我能够实现这一点,但我不知道如何使用openapi生成器

bf1o4zei

bf1o4zei1#

@RequestMapping在Feign客户端早已被劝阻和不良的做法。它导致了CVE-2021-22044,并且由于上述安全问题,在Feign客户端中完全删除了支持@RequestMapping的功能。
话虽如此,基于spring生成器的文档:https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/spring.md
library配置指定为spring-cloud应该会得到您想要的结果。

相关问题