Skip to content

Commit 432cdd7

Browse files
committed
Add ResponseEntity.of(Optional) variant
When dealing with `Optional` values in a Controller handler (for example, values coming from a Spring Data repository), developers might reuse this code snippet quite often: ``` @GetMapping("/user") public ResponseEntity<Optional<User>> fetchUser() { Optional<User> user = //... return user.map(ResponseEntity::ok).orElse(notFound().build()); } ``` This commit adds a new static method on `ResponseEntity` for that, simplifying the previous snippet with `return ResponseEntity.of(user);` Note that in case more specific HTTP response headers are required by the application, developers should use other static methods to explicitly tell which headers should be used in each case. Issue: SPR-17187
1 parent d3be1cc commit 432cdd7

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.net.URI;
2020
import java.util.Arrays;
2121
import java.util.LinkedHashSet;
22+
import java.util.Optional;
2223
import java.util.Set;
2324

2425
import org.springframework.lang.Nullable;
@@ -213,6 +214,18 @@ public static BodyBuilder status(int status) {
213214
return new DefaultBuilder(status);
214215
}
215216

217+
/**
218+
* A shortcut for creating a {@code ResponseEntity} with the given body
219+
* and the {@linkplain HttpStatus#OK OK} status, or an empty body and a
220+
* {@linkplain HttpStatus#NOT_FOUND NOT FOUND} status in case of a
221+
* {@linkplain Optional#empty()} parameter.
222+
* @return the created {@code ResponseEntity}
223+
* @since 5.1
224+
*/
225+
public static <T> ResponseEntity<T> of(Optional<T> body) {
226+
return body.map(ResponseEntity::ok).orElse(notFound().build());
227+
}
228+
216229
/**
217230
* Create a builder with the status set to {@linkplain HttpStatus#OK OK}.
218231
* @return the created builder

spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -19,6 +19,7 @@
1919
import java.net.URI;
2020
import java.net.URISyntaxException;
2121
import java.util.List;
22+
import java.util.Optional;
2223
import java.util.concurrent.TimeUnit;
2324

2425
import org.hamcrest.Matchers;
@@ -72,6 +73,25 @@ public void okEntity() {
7273
assertEquals(entity, responseEntity.getBody());
7374
}
7475

76+
@Test
77+
public void ofOptional() {
78+
Integer entity = 42;
79+
ResponseEntity<Integer> responseEntity = ResponseEntity.of(Optional.of(entity));
80+
81+
assertNotNull(responseEntity);
82+
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
83+
assertEquals(entity, responseEntity.getBody());
84+
}
85+
86+
@Test
87+
public void ofEmptyOptional() {
88+
ResponseEntity<Integer> responseEntity = ResponseEntity.of(Optional.empty());
89+
90+
assertNotNull(responseEntity);
91+
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
92+
assertNull(responseEntity.getBody());
93+
}
94+
7595
@Test
7696
public void createdLocation() throws URISyntaxException {
7797
URI location = new URI("location");

0 commit comments

Comments
 (0)