From d34e472c819a920249a1d4e1e5c6267a1966ea9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Michael?= Date: Fri, 26 Jun 2026 08:49:48 +0200 Subject: [PATCH] Improve nullability in DefaultResponseCreator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Björn Michael --- .../test/web/client/response/DefaultResponseCreator.java | 6 +++--- .../web/client/response/MockRestResponseCreators.java | 9 +++------ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java b/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java index 725a6cb55b18..463922c78eb6 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java @@ -106,7 +106,7 @@ public DefaultResponseCreator body(Resource resource) { /** * Set the {@code Content-Type} header. */ - public DefaultResponseCreator contentType(MediaType mediaType) { + public DefaultResponseCreator contentType(@Nullable MediaType mediaType) { this.headers.setContentType(mediaType); return this; } @@ -114,7 +114,7 @@ public DefaultResponseCreator contentType(MediaType mediaType) { /** * Set the {@code Location} header. */ - public DefaultResponseCreator location(URI location) { + public DefaultResponseCreator location(@Nullable URI location) { this.headers.setLocation(location); return this; } @@ -123,7 +123,7 @@ public DefaultResponseCreator location(URI location) { * Add a response header with one or more values. * @since 6.0 */ - public DefaultResponseCreator header(String name, String ... headerValues) { + public DefaultResponseCreator header(String name, @Nullable String... headerValues) { for (String headerValue : headerValues) { this.headers.add(name, headerValue); } diff --git a/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java b/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java index 06530b2239a6..ff5d490b8cc4 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java @@ -54,8 +54,7 @@ public static DefaultResponseCreator withSuccess() { * @param contentType the type of the content (may be {@code null}) */ public static DefaultResponseCreator withSuccess(String body, @Nullable MediaType contentType) { - DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(body); - return (contentType != null ? creator.contentType(contentType) : creator); + return new DefaultResponseCreator(HttpStatus.OK).body(body).contentType(contentType); } /** @@ -64,8 +63,7 @@ public static DefaultResponseCreator withSuccess(String body, @Nullable MediaTyp * @param contentType the type of the content (may be {@code null}) */ public static DefaultResponseCreator withSuccess(byte[] body, @Nullable MediaType contentType) { - DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(body); - return (contentType != null ? creator.contentType(contentType) : creator); + return new DefaultResponseCreator(HttpStatus.OK).body(body).contentType(contentType); } /** @@ -74,8 +72,7 @@ public static DefaultResponseCreator withSuccess(byte[] body, @Nullable MediaTyp * @param contentType the type of the content (may be {@code null}) */ public static DefaultResponseCreator withSuccess(Resource body, @Nullable MediaType contentType) { - DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(body); - return (contentType != null ? creator.contentType(contentType) : creator); + return new DefaultResponseCreator(HttpStatus.OK).body(body).contentType(contentType); } /**