gpt4 book ai didi

java - SpringBoot 未加载使用 OpenAPI 3.0 生成的 Controller

转载 作者:行者123 更新时间:2023-12-01 16:31:54 25 4
gpt4 key购买 nike

我正在使用openapi-generator-maven-plugin生成代码。代码已成功生成。但 Spring Boot 并未在上下文中加载生成的代码。我不知道为什么,因为我做了以下事情。

以下是我的插件配置:

<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/spec/SchoolAPIs.v1.yaml</inputSpec>
<output>${project.build.directory}/server</output>
<language>spring</language>
<apiPackage>com.school.rest.controllers</apiPackage>
<modelPackage>com.school.rest.generated.dtos</modelPackage>
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
<library>spring-boot</library>
<configOptions>
<java8>false</java8>
<dateLibrary>java8</dateLibrary>
<delegatePattern>true</delegatePattern>
<useBeanValidation>true</useBeanValidation>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>

下面是我的主类,我在其中指定了配置:

@EnableSwagger2
@EnableAsync
@SpringBootApplication(exclude = SecurityAutoConfiguration.class, scanBasePackages = { "com.school.rest"})
@EnableJpaRepositories(basePackages = "com.school.rest.repository")
@Slf4j
public class SchoolAPIApplication implements CommandLineRunner {

public static void main(String[] args) {
ApplicationContext context = new SpringApplicationBuilder(SchoolAPIApplication.class)
.registerShutdownHook(true).run(args);
PlansApiController plansApiController = context.getBean(PlansApiController.class);
log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>> {}", plansApiController);
}

}

我不确定这里出了什么问题,已经为此苦苦挣扎了近两天。无法弄清楚缺少什么。

如果您有任何可用的解决方案,请告诉我。

更新:此调用会导致以下错误>

log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>> {}", plansApiController);

错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type...

问候

最佳答案

据我所知,您生成的包在启动时没有被扫描。我尝试在这里重新创建您的问题和解决方案https://github.com/tinashehondo/notifications-api

<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.0.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
<output>${project.build.directory}/api</output>
<language>spring</language>
<modelPackage>uk.co.tinashehondo.notifications.rest.model</modelPackage>
<apiPackage>uk.co.tinashehondo.notifications.rest.api</apiPackage>
<generateSupportingFiles>true</generateSupportingFiles>
<configOptions>
<java8>false</java8>
<dateLibrary>java8</dateLibrary>
<delegatePattern>true</delegatePattern>
<useBeanValidation>true</useBeanValidation>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>

还有

@SpringBootApplication(exclude = SecurityAutoConfiguration.class,
scanBasePackages = { "uk.co.tinashehondo.notifications.rest"})
public class App {
public static void main(String[] args) {
ApplicationContext context = new SpringApplicationBuilder(App.class)
.registerShutdownHook(true).run(args);
NotificationApi notificationApiController = context.getBean(NotificationApi.class);
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> {}"+notificationApiController); //forgive sys.out here
}

}

输出

>>>>>>>>>>>>>>>>>>>>>>>>>>>>     {}uk.co.tinashehondo.notifications.rest.api.NotificationApiController@7b948f3e

关于java - SpringBoot 未加载使用 OpenAPI 3.0 生成的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62021727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com