1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
69
69
* @since 3.0.2
70
70
* @param <T> the body type
71
71
* @see #getStatusCode()
72
+ * @see org.springframework.web.client.RestOperations#getForEntity(String, Class, Object...)
73
+ * @see org.springframework.web.client.RestOperations#getForEntity(String, Class, java.util.Map)
74
+ * @see org.springframework.web.client.RestOperations#getForEntity(URI, Class)
75
+ * @see RequestEntity
72
76
*/
73
77
public class ResponseEntity <T > extends HttpEntity <T > {
74
78
@@ -216,19 +220,6 @@ public static BodyBuilder status(int status) {
216
220
return new DefaultBuilder (status );
217
221
}
218
222
219
- /**
220
- * A shortcut for creating a {@code ResponseEntity} with the given body
221
- * and the {@linkplain HttpStatus#OK OK} status, or an empty body and a
222
- * {@linkplain HttpStatus#NOT_FOUND NOT FOUND} status in case of a
223
- * {@linkplain Optional#empty()} parameter.
224
- * @return the created {@code ResponseEntity}
225
- * @since 5.1
226
- */
227
- public static <T > ResponseEntity <T > of (Optional <T > body ) {
228
- Assert .notNull (body , "Body must not be null" );
229
- return body .map (ResponseEntity ::ok ).orElse (notFound ().build ());
230
- }
231
-
232
223
/**
233
224
* Create a builder with the status set to {@linkplain HttpStatus#OK OK}.
234
225
* @return the created builder
@@ -245,8 +236,20 @@ public static BodyBuilder ok() {
245
236
* @since 4.1
246
237
*/
247
238
public static <T > ResponseEntity <T > ok (T body ) {
248
- BodyBuilder builder = ok ();
249
- return builder .body (body );
239
+ return ok ().body (body );
240
+ }
241
+
242
+ /**
243
+ * A shortcut for creating a {@code ResponseEntity} with the given body
244
+ * and the {@linkplain HttpStatus#OK OK} status, or an empty body and a
245
+ * {@linkplain HttpStatus#NOT_FOUND NOT FOUND} status in case of an
246
+ * {@linkplain Optional#empty()} parameter.
247
+ * @return the created {@code ResponseEntity}
248
+ * @since 5.1
249
+ */
250
+ public static <T > ResponseEntity <T > of (Optional <T > body ) {
251
+ Assert .notNull (body , "Body must not be null" );
252
+ return body .map (ResponseEntity ::ok ).orElseGet (() -> notFound ().build ());
250
253
}
251
254
252
255
/**
@@ -257,8 +260,7 @@ public static <T> ResponseEntity<T> ok(T body) {
257
260
* @since 4.1
258
261
*/
259
262
public static BodyBuilder created (URI location ) {
260
- BodyBuilder builder = status (HttpStatus .CREATED );
261
- return builder .location (location );
263
+ return status (HttpStatus .CREATED ).location (location );
262
264
}
263
265
264
266
/**
0 commit comments