Skip to content

Commit f965c2f

Browse files
committed
Avoid unnecessary creation of not-found entity in ResponseEntity#of
Closes gh-25183
1 parent cf5bbce commit f965c2f

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

spring-web/src/main/java/org/springframework/http/ResponseEntity.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,6 +69,10 @@
6969
* @since 3.0.2
7070
* @param <T> the body type
7171
* @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
7276
*/
7377
public class ResponseEntity<T> extends HttpEntity<T> {
7478

@@ -216,19 +220,6 @@ public static BodyBuilder status(int status) {
216220
return new DefaultBuilder(status);
217221
}
218222

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-
232223
/**
233224
* Create a builder with the status set to {@linkplain HttpStatus#OK OK}.
234225
* @return the created builder
@@ -245,8 +236,20 @@ public static BodyBuilder ok() {
245236
* @since 4.1
246237
*/
247238
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());
250253
}
251254

252255
/**
@@ -257,8 +260,7 @@ public static <T> ResponseEntity<T> ok(T body) {
257260
* @since 4.1
258261
*/
259262
public static BodyBuilder created(URI location) {
260-
BodyBuilder builder = status(HttpStatus.CREATED);
261-
return builder.location(location);
263+
return status(HttpStatus.CREATED).location(location);
262264
}
263265

264266
/**

0 commit comments

Comments
 (0)