Browse Source

Add `@Nullable` to ResponseEntity generic type

Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
pull/36238/head
Tran Ngoc Nhan 2 months ago
parent
commit
dc3a495b53
  1. 2
      spring-web/src/main/java/org/springframework/http/HttpEntity.java
  2. 8
      spring-web/src/main/java/org/springframework/http/ResponseEntity.java
  3. 17
      spring-web/src/test/kotlin/org/springframework/http/ResponseEntityKotlinTests.kt

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

@ -56,7 +56,7 @@ import org.springframework.util.ObjectUtils; @@ -56,7 +56,7 @@ import org.springframework.util.ObjectUtils;
* @see #getBody()
* @see #getHeaders()
*/
public class HttpEntity<T> {
public class HttpEntity<T extends @Nullable Object> {
/**
* An {@code HttpEntity} instance with a {@code null} body and

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

@ -78,7 +78,7 @@ import org.springframework.util.ObjectUtils; @@ -78,7 +78,7 @@ import org.springframework.util.ObjectUtils;
* @see org.springframework.web.client.RestOperations#getForEntity(URI, Class)
* @see RequestEntity
*/
public class ResponseEntity<T> extends HttpEntity<T> {
public class ResponseEntity<T extends @Nullable Object> extends HttpEntity<T> {
private final HttpStatusCode status;
@ -261,7 +261,7 @@ public class ResponseEntity<T> extends HttpEntity<T> { @@ -261,7 +261,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
* @return the created {@code ResponseEntity}
* @since 4.1
*/
public static <T> ResponseEntity<T> ok(@Nullable T body) {
public static <T extends @Nullable Object> ResponseEntity<T> ok(@Nullable T body) {
return ok().body(body);
}
@ -308,7 +308,7 @@ public class ResponseEntity<T> extends HttpEntity<T> { @@ -308,7 +308,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
* @return the created {@code ResponseEntity}
* @since 6.0.5
*/
public static <T> ResponseEntity<T> ofNullable(@Nullable T body) {
public static <T extends @Nullable Object> ResponseEntity<T> ofNullable(@Nullable T body) {
if (body == null) {
return notFound().build();
}
@ -658,7 +658,7 @@ public class ResponseEntity<T> extends HttpEntity<T> { @@ -658,7 +658,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
}
@Override
public <T> ResponseEntity<T> body(@Nullable T body) {
public <T extends @Nullable Object> ResponseEntity<T> body(@Nullable T body) {
return new ResponseEntity<>(body, this.headers, this.statusCode);
}
}

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

@ -43,4 +43,21 @@ class ResponseEntityKotlinTests { @@ -43,4 +43,21 @@ class ResponseEntityKotlinTests {
assertThat(responseEntity.body).isNull()
}
@Test
fun ofNullNullableType() {
val responseEntity = ResponseEntity.ofNullable<Int?>(null)
assertThat(responseEntity).isNotNull()
assertThat(responseEntity.statusCode).isEqualTo(HttpStatus.NOT_FOUND)
assertThat(responseEntity.body).isNull()
}
@Test
fun okNullNullableType() {
val responseEntity = ResponseEntity.ok<String?>(null)
assertThat(responseEntity).isNotNull()
assertThat(responseEntity.statusCode).isEqualTo(HttpStatus.OK)
assertThat(responseEntity.body).isNull()
}
}

Loading…
Cancel
Save