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(); } + }