You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot无法识别OpenAPI生成的控制器问题排查与解决

使用openapi-generator-maven-plugin生成的Spring控制器接口无法被识别,返回404

我通过openapi-generator-maven-plugin生成了控制器接口,编写实现类实现该接口并调用实际服务后,启动应用或执行MVC测试时,请求该控制器始终返回HTTP 404,Spring无法识别这些控制器。

插件配置(pom.xml)

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>${openapi-generator-maven-plugin.version}</version>
    <executions>
        <execution>
            <id>generate-api</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>
                    ${project.basedir}/src/main/resources/openapi/notification-settings/notification-settings-controller.yaml
                </inputSpec>

                <generatorName>spring</generatorName>
                <library>spring-boot</library>
                <generateApis>true</generateApis>
                <generateApiTests>false</generateApiTests>
                <generateModels>true</generateModels>
                <generateModelTests>false</generateModelTests>

                <apiPackage>com.epam.stts.api</apiPackage>
                <modelPackage>com.epam.stts.model</modelPackage>

                <importMappings>
                    ChannelType=com.epam.stts.type.ChannelType,
                    NotificationSettingsType=com.epam.stts.type.NotificationSettingsType,
                    Period=com.epam.stts.component.Period
                </importMappings>

                <configOptions>
                    <interfaceOnly>true</interfaceOnly>
                    <serializableModel>true</serializableModel>
                    <skipDefaultInterface>true</skipDefaultInterface>
                    <hideGenerationTimestamp>true</hideGenerationTimestamp>
                    <useOptional>false</useOptional>

                    <additionalModelTypeAnnotations>
                        @lombok.Data
                        @lombok.NoArgsConstructor
                        @lombok.AllArgsConstructor
                        @lombok.Builder
                    </additionalModelTypeAnnotations>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

生成的接口代码

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
@Validated
@Tag(name = "notification-settings", description = "the notification-settings API")
public interface NotificationSettingsApi {

    /**
     * GET /notification-settings : Get user&#39;s notification settings
     *
     * @param userId ID of user (required)
     * @return OK (status code 200)
     */
    @Operation(
        operationId = "getUserNotificationSettings",
        summary = "Get user's notification settings",
        responses = {
            @ApiResponse(responseCode = "200", description = "OK", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = NotificationSettingsInfo.class))
            })
        }
    )
    @RequestMapping(
        method = RequestMethod.GET,
        value = "/notification-settings",
        produces = { "application/json" }
    )
    ResponseEntity<List<NotificationSettingsInfo>> getUserNotificationSettings(
        @Parameter(name = "userId", description = "ID of user", required = true) @PathVariable("userId") Long userId
    );


    /**
     * POST /notification-settings/save : Save notification settings
     *
     * @param notificationSettingsInfo NotificationSettingsInfo request body (optional)
     * @return OK (status code 200)
     *         or Validation exception while mapping request body (status code 400)
     */
    @Operation(
        operationId = "saveNotificationSettings",
        summary = "Save notification settings",
        responses = {
            @ApiResponse(responseCode = "200", description = "OK", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = Object.class))
            }),
            @ApiResponse(responseCode = "400", description = "Validation exception while mapping request body", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = Object.class))
            })
        }
    )
    @RequestMapping(
        method = RequestMethod.POST,
        value = "/notification-settings/save",
        produces = { "application/json" },
        consumes = { "application/json" }
    )
    ResponseEntity<Object> saveNotificationSettings(
        @Parameter(name = "NotificationSettingsInfo", description = "NotificationSettingsInfo request body") @Valid @RequestBody(required = false) NotificationSettingsInfo notificationSettingsInfo
    );

}

解决方法

在插件配置的<configOptions>节点中添加<useSpringController>true</useSpringController>配置,让生成的接口自动带上@Controller注解,Spring就能正常识别并映射请求了,目前功能已恢复正常。

内容的提问来源于stack exchange,提问作者Andy Krylov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 17:12:22