From c2cf5e065def0859fc910d46fc7ac14c3cfbb750 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Fri, 10 Apr 2026 15:00:13 +0200 Subject: [PATCH] Perform case-insensitive lookup in HttpMethod.valueOf() Prior to this commit, the implementation of HttpMethod.valueOf() aligned with the semantics of Enum#valueOf() which requires an exact match for the enum constant name. However, since HttpMethod is no longer an enum, that restriction is no longer necessary. Consequently, this commit revises the implementation of valueOf() to perform a case-insensitive lookup for predefined constants. In other words, HttpMethod.valueOf("GET") and HttpMethod.valueOf("get") now both resolve to HttpMethod.GET. Closes gh-36518 --- .../java/org/springframework/http/HttpMethod.java | 6 +++++- .../org/springframework/http/HttpMethodTests.java | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/HttpMethod.java b/spring-web/src/main/java/org/springframework/http/HttpMethod.java index 608d7d535a1..bf5843aeda7 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpMethod.java +++ b/spring-web/src/main/java/org/springframework/http/HttpMethod.java @@ -17,6 +17,7 @@ package org.springframework.http; import java.io.Serializable; +import java.util.Locale; import org.jspecify.annotations.Nullable; @@ -29,6 +30,7 @@ import org.springframework.util.Assert; * * @author Arjen Poutsma * @author Juergen Hoeller + * @author Sam Brannen * @since 3.0 */ public final class HttpMethod implements Comparable, Serializable { @@ -110,12 +112,14 @@ public final class HttpMethod implements Comparable, Serializable { /** * Return an {@code HttpMethod} object for the given value. + *

As of Spring Framework 7.1, lookups for predefined constants such as + * {@link HttpMethod#GET GET} are case-insensitive. * @param method the method value as a String * @return the corresponding {@code HttpMethod} */ public static HttpMethod valueOf(String method) { Assert.notNull(method, "Method must not be null"); - return switch (method) { + return switch (method.toUpperCase(Locale.ROOT)) { case "GET" -> GET; case "HEAD" -> HEAD; case "POST" -> POST; diff --git a/spring-web/src/test/java/org/springframework/http/HttpMethodTests.java b/spring-web/src/test/java/org/springframework/http/HttpMethodTests.java index 1fb6205aad7..ac3a3f0ae4e 100644 --- a/spring-web/src/test/java/org/springframework/http/HttpMethodTests.java +++ b/spring-web/src/test/java/org/springframework/http/HttpMethodTests.java @@ -57,8 +57,15 @@ class HttpMethodTests { HttpMethod get = HttpMethod.valueOf("GET"); assertThat(get).isSameAs(HttpMethod.GET); - HttpMethod foo = HttpMethod.valueOf("FOO"); - HttpMethod other = HttpMethod.valueOf("FOO"); + get = HttpMethod.valueOf("Get"); + assertThat(get).isSameAs(HttpMethod.GET); + + get = HttpMethod.valueOf("get"); + assertThat(get).isSameAs(HttpMethod.GET); + + HttpMethod foo = HttpMethod.valueOf("foo"); + HttpMethod other = HttpMethod.valueOf("foo"); + assertThat(foo).isNotSameAs(other); assertThat(foo).isEqualTo(other); } @@ -73,4 +80,5 @@ class HttpMethodTests { assertThat(HttpMethod.GET.matches("GET")).isTrue(); assertThat(HttpMethod.GET.matches("FOO")).isFalse(); } + }