【问题标题】:OpenApi 3 codegen java how to implementOpenApi 3 codegen java如何实现
【发布时间】:2020-04-29 19:52:15
【问题描述】:

有一个 openapi yml 文件,其中包含一个简单的 get 请求和响应,从这个 yml 生成下面的 java 文件(以及其他文件)。

应该如何使用这些java文件? 如何挂钩生成的文件?

可以简单地将生成的主类和控制器类复制到主源树中,但似乎不是正确的方法。

--- 编辑---

在生成的Controller类中,如何覆盖ExampleApi接口中存在的默认响应?无需修改生成的 Controller 类并将其放在 VCS 中。

--- 编辑---

build.gradle.kts

...
openApiGenerate {
    generatorName.set("spring")

    inputSpec.set("$rootDir/specs/api-example.yml")
    outputDir.set("$buildDir/generated")

    apiPackage.set("com.example.openapi.generated.api")
    invokerPackage.set("com.example.openapi.generated.invoker")
    modelPackage.set("com.example.openapi.generated.model")

    configOptions.set(mapOf(
            "dateLibrary" to "java8"
    ))
    systemProperties.set(mapOf(
            "modelDocs" to "false"
    ))
}
...

这些类应该做什么?

package com.example.openapi.generated.invoker;

import com.fasterxml.jackson.databind.Module;
import org.openapitools.jackson.nullable.JsonNullableModule;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.openapi.generated.invoker", "com.example.openapi.generated.api" , "org.openapitools.configuration"})
public class OpenAPI2SpringBoot implements CommandLineRunner {

    @Override
    public void run(String... arg0) throws Exception {
        if (arg0.length > 0 && arg0[0].equals("exitcode")) {
            throw new ExitException();
        }
    }

    public static void main(String[] args) throws Exception {
        new SpringApplication(OpenAPI2SpringBoot.class).run(args);
    }

    static class ExitException extends RuntimeException implements ExitCodeGenerator {
        private static final long serialVersionUID = 1L;

        @Override
        public int getExitCode() {
            return 10;
        }

    }

    @Bean
    public WebMvcConfigurer webConfigurer() {
        return new WebMvcConfigurer() {
            /*@Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedMethods("*")
                        .allowedHeaders("Content-Type");
            }*/
        };
    }

    @Bean
    public Module jsonNullableModule() {
        return new JsonNullableModule();
    }

}

package com.example.openapi.generated.api;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.NativeWebRequest;
import java.util.Optional;

@Controller
@RequestMapping("${openapi.basic.base-path:}")
public class ExampleApiController implements ExampleApi {

    private final NativeWebRequest request;

    @org.springframework.beans.factory.annotation.Autowired
    public ExampleApiController(NativeWebRequest request) {
        this.request = request;
    }

    @Override
    public Optional<NativeWebRequest> getRequest() {
        return Optional.ofNullable(request);
    }

}

【问题讨论】:

    标签: java spring-boot gradle openapi openapi-generator


    【解决方案1】:

    你需要告诉 gradle 编译文件,不需要复制它们。

    将生成文件的路径添加到项目的sourceSets。像这样的:

    sourceSets {
        main {
            java {
                srcDir("$buildDir/generated")
            }
        }
    }
    

    【讨论】:

    • 生成的类已经是sourceSets的一部分,但是如何自定义一个请求的响应呢?在生成的Controller类中,如何覆盖ExampleApi接口中存在的默认响应?
    • 你可以扩展生成的控制器,或者告诉生成器只创建接口并实现接口。
    • 通过只生成接口并自己实现控制器解决了这个问题。
    • 如何告诉 openapi 使用我的实现而不是它创建的实现?现在我只是手动编辑生成的文件来添加我的接口实现
    • spring settings 中有一个interfaceOnly 配置选项。
    猜你喜欢
    • 1970-01-01
    • 2022-06-25
    • 1970-01-01
    • 2017-04-15
    • 2020-05-09
    • 2022-01-04
    • 2021-12-22
    • 2021-04-01
    • 2020-08-22
    相关资源
    最近更新 更多