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

从Java 11升级到Java 17时Switch语句预览特性报错的解决方案咨询

问题描述

将项目从Java 11升级到Java 17,同时从Spring Boot 2.7.8升级到Spring Boot 3.0.0后,测试类中的如下方法运行报错:

protected void stub(HttpMethod method, String url, ResponseDefinitionBuilder response) {
    switch (method) {
        case GET:
            wireMockServer.stubFor(get(urlMatching(url)).willReturn(response));
            break;
        case POST:
            wireMockServer.stubFor(post(urlMatching(url)).willReturn(response));
            break;
        case PUT:
            wireMockServer.stubFor(put(urlMatching(url)).willReturn(response));
            break;
        case DELETE:
            wireMockServer.stubFor(delete(urlMatching(url)).willReturn(response));
            break;
        default:
            throw new RuntimeException("invalid http method");
    }
}

pom.xml核心配置如下:

<properties>
    <java.version>17</java.version>
</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.0</version>
    <relativePath/>
</parent>

报错信息:

patterns in switch statements are a preview feature and are disabled by default.

解决方案

方案一:启用Java 17预览特性编译参数

报错根源是升级后依赖的库(如WireMock)内部使用了Java 17的switch模式匹配预览特性,但项目编译/运行时未开启预览特性支持。在pom.xml中添加以下插件配置,启用预览特性:

<build>
    <plugins>
        <!-- 配置编译插件,启用预览特性 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <release>17</release>
                <compilerArgs>
                    <arg>--enable-preview</arg>
                </compilerArgs>
            </configuration>
        </plugin>
        <!-- 配置测试运行插件,启用预览特性 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
            <configuration>
                <argLine>--enable-preview</argLine>
            </configuration>
        </plugin>
    </plugins>
</build>

方案二:重构代码替换switch语句

改用Map映射HttpMethod到对应的WireMock请求构建方法,既规避预览特性冲突,也让代码更简洁:

import static com.github.tomakehurst.wiremock.client.WireMock.*;

// 提前定义映射关系
private static final Map<HttpMethod, Function<String, RequestBuilder>> METHOD_HANDLERS = Map.of(
    HttpMethod.GET, url -> get(urlMatching(url)),
    HttpMethod.POST, url -> post(urlMatching(url)),
    HttpMethod.PUT, url -> put(urlMatching(url)),
    HttpMethod.DELETE, url -> delete(urlMatching(url))
);

protected void stub(HttpMethod method, String url, ResponseDefinitionBuilder response) {
    RequestBuilder requestBuilder = METHOD_HANDLERS.get(method);
    if (requestBuilder == null) {
        throw new RuntimeException("invalid http method");
    }
    wireMockServer.stubFor(requestBuilder.apply(url).willReturn(response));
}

内容的提问来源于stack exchange,提问作者Ivajlo Iliev

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 03:11:25