From dc3a495b53828704576b414d49d797379ee3ab4a Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Sat, 31 Jan 2026 19:28:48 +0700 Subject: [PATCH] Add `@Nullable` to ResponseEntity generic type Signed-off-by: Tran Ngoc Nhan --- .../org/springframework/http/HttpEntity.java | 2 +- .../springframework/http/ResponseEntity.java | 8 ++++---- .../http/ResponseEntityKotlinTests.kt | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/HttpEntity.java b/spring-web/src/main/java/org/springframework/http/HttpEntity.java index d55a6b89172..0323d78b191 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpEntity.java +++ b/spring-web/src/main/java/org/springframework/http/HttpEntity.java @@ -56,7 +56,7 @@ import org.springframework.util.ObjectUtils; * @see #getBody() * @see #getHeaders() */ -public class HttpEntity { +public class HttpEntity { /** * An {@code HttpEntity} instance with a {@code null} body and diff --git a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java index 9d879cf5526..6373ba88d6d 100644 --- a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java +++ b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java @@ -78,7 +78,7 @@ import org.springframework.util.ObjectUtils; * @see org.springframework.web.client.RestOperations#getForEntity(URI, Class) * @see RequestEntity */ -public class ResponseEntity extends HttpEntity { +public class ResponseEntity extends HttpEntity { private final HttpStatusCode status; @@ -261,7 +261,7 @@ public class ResponseEntity extends HttpEntity { * @return the created {@code ResponseEntity} * @since 4.1 */ - public static ResponseEntity ok(@Nullable T body) { + public static ResponseEntity ok(@Nullable T body) { return ok().body(body); } @@ -308,7 +308,7 @@ public class ResponseEntity extends HttpEntity { * @return the created {@code ResponseEntity} * @since 6.0.5 */ - public static ResponseEntity ofNullable(@Nullable T body) { + public static ResponseEntity ofNullable(@Nullable T body) { if (body == null) { return notFound().build(); } @@ -658,7 +658,7 @@ public class ResponseEntity extends HttpEntity { } @Override - public ResponseEntity body(@Nullable T body) { + public ResponseEntity body(@Nullable T body) { return new ResponseEntity<>(body, this.headers, this.statusCode); } } diff --git a/spring-web/src/test/kotlin/org/springframework/http/ResponseEntityKotlinTests.kt b/spring-web/src/test/kotlin/org/springframework/http/ResponseEntityKotlinTests.kt index cd90622f699..a59389a2d22 100644 --- a/spring-web/src/test/kotlin/org/springframework/http/ResponseEntityKotlinTests.kt +++ b/spring-web/src/test/kotlin/org/springframework/http/ResponseEntityKotlinTests.kt @@ -43,4 +43,21 @@ class ResponseEntityKotlinTests { assertThat(responseEntity.body).isNull() } + + @Test + fun ofNullNullableType() { + val responseEntity = ResponseEntity.ofNullable(null) + assertThat(responseEntity).isNotNull() + assertThat(responseEntity.statusCode).isEqualTo(HttpStatus.NOT_FOUND) + assertThat(responseEntity.body).isNull() + } + + @Test + fun okNullNullableType() { + val responseEntity = ResponseEntity.ok(null) + assertThat(responseEntity).isNotNull() + assertThat(responseEntity.statusCode).isEqualTo(HttpStatus.OK) + assertThat(responseEntity.body).isNull() + } + }