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
This commit is contained in:
Sam Brannen
2026-04-10 15:04:42 +02:00
parent e0e78257d6
commit c2cf5e065d
2 changed files with 15 additions and 3 deletions
@@ -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<HttpMethod>, Serializable {
@@ -110,12 +112,14 @@ public final class HttpMethod implements Comparable<HttpMethod>, Serializable {
/**
* Return an {@code HttpMethod} object for the given value.
* <p>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;
@@ -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();
}
}