16
16
17
17
package org .springframework .http ;
18
18
19
+ import java .io .Serializable ;
19
20
import java .util .HashMap ;
20
21
import java .util .Map ;
21
22
22
23
import org .springframework .lang .Nullable ;
24
+ import org .springframework .util .Assert ;
23
25
24
26
/**
25
- * Java 5 enumeration of HTTP request methods. Intended for use
27
+ * Represents an HTTP request methods. Intended for use
26
28
* with {@link org.springframework.http.client.ClientHttpRequest}
27
29
* and {@link org.springframework.web.client.RestTemplate}.
28
30
*
29
31
* @author Arjen Poutsma
30
32
* @author Juergen Hoeller
31
33
* @since 3.0
32
34
*/
33
- public enum HttpMethod {
35
+ public final class HttpMethod implements Comparable < HttpMethod >, Serializable {
34
36
35
- GET , HEAD , POST , PUT , PATCH , DELETE , OPTIONS , TRACE ;
37
+ private static final long serialVersionUID = - 70133475680645360L ;
36
38
39
+ private static final HttpMethod [] values ;
37
40
38
41
private static final Map <String , HttpMethod > mappings = new HashMap <>(16 );
39
42
43
+
44
+ /**
45
+ * The HTTP method {@code GET}.
46
+ * @see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3">HTTP 1.1, section 9.3</a>
47
+ */
48
+ public static final HttpMethod GET = new HttpMethod ("GET" );
49
+
50
+ /**
51
+ * The HTTP method {@code HEAD}.
52
+ * @see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4">HTTP 1.1, section 9.4</a>
53
+ */
54
+ public static final HttpMethod HEAD = new HttpMethod ("HEAD" );
55
+
56
+ /**
57
+ * The HTTP method {@code POST}.
58
+ * @see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5">HTTP 1.1, section 9.5</a>
59
+ */
60
+ public static final HttpMethod POST = new HttpMethod ("POST" );
61
+
62
+ /**
63
+ * The HTTP method {@code PUT}.
64
+ * @see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6">HTTP 1.1, section 9.6</a>
65
+ */
66
+ public static final HttpMethod PUT = new HttpMethod ("PUT" );
67
+
68
+ /**
69
+ * The HTTP method {@code PATCH}.
70
+ * @see <a href="https://datatracker.ietf.org/doc/html/rfc5789#section-2">RFC 5789</a>
71
+ */
72
+ public static final HttpMethod PATCH = new HttpMethod ("PATCH" );
73
+
74
+ /**
75
+ * The HTTP method {@code DELETE}.
76
+ * @see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7">HTTP 1.1, section 9.7</a>
77
+ */
78
+ public static final HttpMethod DELETE = new HttpMethod ("DELETE" );
79
+
80
+ /**
81
+ * The HTTP method {@code OPTIONS}.
82
+ * @see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.2">HTTP 1.1, section 9.2</a>
83
+ */
84
+ public static final HttpMethod OPTIONS = new HttpMethod ("OPTIONS" );
85
+
86
+ /**
87
+ * The HTTP method {@code TRACE}.
88
+ * @see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.8">HTTP 1.1, section 9.8</a>
89
+ */
90
+ public static final HttpMethod TRACE = new HttpMethod ("TRACE" );
91
+
92
+
40
93
static {
41
- for (HttpMethod httpMethod : values ()) {
94
+ values = new HttpMethod []{GET , HEAD , POST , PUT , PATCH , DELETE , OPTIONS , TRACE };
95
+ for (HttpMethod httpMethod : values ) {
42
96
mappings .put (httpMethod .name (), httpMethod );
43
97
}
44
98
}
45
99
46
100
101
+ private final String name ;
102
+
103
+
104
+ private HttpMethod (String name ) {
105
+ this .name = name ;
106
+ }
107
+
108
+ /**
109
+ * Returns an array containing the standard HTTP methods. Specifically,
110
+ * this method returns an array containing {@link #GET}, {@link #HEAD},
111
+ * {@link #POST}, {@link #PUT}, {@link #PATCH}, {@link #DELETE},
112
+ * {@link #OPTIONS}, and {@link #TRACE}.
113
+ *
114
+ * <p>Note that the returned value does not include any HTTP methods defined
115
+ * in WebDav.
116
+ */
117
+ public static HttpMethod [] values () {
118
+ HttpMethod [] copy = new HttpMethod [values .length ];
119
+ System .arraycopy (values , 0 , copy , 0 , values .length );
120
+ return copy ;
121
+ }
122
+
123
+ /**
124
+ * Return an {@code HttpMethod} object for the given value.
125
+ * @param method the method value as a String
126
+ * @return the corresponding {@code HttpMethod}
127
+ */
128
+ public static HttpMethod valueOf (String method ) {
129
+ Assert .notNull (method , "Name must not be null" );
130
+ HttpMethod result = mappings .get (method );
131
+ if (result != null ) {
132
+ return result ;
133
+ }
134
+ else {
135
+ return new HttpMethod (method );
136
+ }
137
+ }
138
+
47
139
/**
48
140
* Resolve the given method value to an {@code HttpMethod}.
49
141
* @param method the method value as a String
50
142
* @return the corresponding {@code HttpMethod}, or {@code null} if not found
51
143
* @since 4.2.4
144
+ * @deprecated in favor of {@link #valueOf(String)}
52
145
*/
53
146
@ Nullable
147
+ @ Deprecated
54
148
public static HttpMethod resolve (@ Nullable String method ) {
55
149
return (method != null ? mappings .get (method ) : null );
56
150
}
57
151
58
152
153
+ /**
154
+ * Return the name of this method, e.g. "GET", "POST".
155
+ */
156
+ public String name () {
157
+ return this .name ;
158
+ }
159
+
59
160
/**
60
161
* Determine whether this {@code HttpMethod} matches the given method value.
61
162
* @param method the HTTP method as a String
@@ -66,4 +167,30 @@ public boolean matches(String method) {
66
167
return name ().equals (method );
67
168
}
68
169
170
+
171
+ @ Override
172
+ public int compareTo (HttpMethod other ) {
173
+ return this .name .compareTo (other .name );
174
+ }
175
+
176
+ @ Override
177
+ public int hashCode () {
178
+ return this .name .hashCode ();
179
+ }
180
+
181
+ @ Override
182
+ public boolean equals (Object o ) {
183
+ if (this == o ) {
184
+ return true ;
185
+ }
186
+ else if (o instanceof HttpMethod other ) {
187
+ return this .name .equals (other .name );
188
+ }
189
+ return false ;
190
+ }
191
+
192
+ @ Override
193
+ public String toString () {
194
+ return this .name ;
195
+ }
69
196
}
0 commit comments