Skip to content

Commit dc3a495

Browse files
Add @Nullable to ResponseEntity generic type
Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
1 parent ab314aa commit dc3a495

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
* @see #getBody()
5757
* @see #getHeaders()
5858
*/
59-
public class HttpEntity<T> {
59+
public class HttpEntity<T extends @Nullable Object> {
6060

6161
/**
6262
* An {@code HttpEntity} instance with a {@code null} body and

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
* @see org.springframework.web.client.RestOperations#getForEntity(URI, Class)
7979
* @see RequestEntity
8080
*/
81-
public class ResponseEntity<T> extends HttpEntity<T> {
81+
public class ResponseEntity<T extends @Nullable Object> extends HttpEntity<T> {
8282

8383
private final HttpStatusCode status;
8484

@@ -261,7 +261,7 @@ public static BodyBuilder ok() {
261261
* @return the created {@code ResponseEntity}
262262
* @since 4.1
263263
*/
264-
public static <T> ResponseEntity<T> ok(@Nullable T body) {
264+
public static <T extends @Nullable Object> ResponseEntity<T> ok(@Nullable T body) {
265265
return ok().body(body);
266266
}
267267

@@ -308,7 +308,7 @@ public <T> ResponseEntity<T> build() {
308308
* @return the created {@code ResponseEntity}
309309
* @since 6.0.5
310310
*/
311-
public static <T> ResponseEntity<T> ofNullable(@Nullable T body) {
311+
public static <T extends @Nullable Object> ResponseEntity<T> ofNullable(@Nullable T body) {
312312
if (body == null) {
313313
return notFound().build();
314314
}
@@ -658,7 +658,7 @@ public <T> ResponseEntity<T> build() {
658658
}
659659

660660
@Override
661-
public <T> ResponseEntity<T> body(@Nullable T body) {
661+
public <T extends @Nullable Object> ResponseEntity<T> body(@Nullable T body) {
662662
return new ResponseEntity<>(body, this.headers, this.statusCode);
663663
}
664664
}

spring-web/src/test/kotlin/org/springframework/http/ResponseEntityKotlinTests.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,21 @@ class ResponseEntityKotlinTests {
4343
assertThat(responseEntity.body).isNull()
4444
}
4545

46+
47+
@Test
48+
fun ofNullNullableType() {
49+
val responseEntity = ResponseEntity.ofNullable<Int?>(null)
50+
assertThat(responseEntity).isNotNull()
51+
assertThat(responseEntity.statusCode).isEqualTo(HttpStatus.NOT_FOUND)
52+
assertThat(responseEntity.body).isNull()
53+
}
54+
55+
@Test
56+
fun okNullNullableType() {
57+
val responseEntity = ResponseEntity.ok<String?>(null)
58+
assertThat(responseEntity).isNotNull()
59+
assertThat(responseEntity.statusCode).isEqualTo(HttpStatus.OK)
60+
assertThat(responseEntity.body).isNull()
61+
}
62+
4663
}

0 commit comments

Comments
 (0)