diff --git a/spring-web/src/main/java/org/springframework/http/ProblemDetail.java b/spring-web/src/main/java/org/springframework/http/ProblemDetail.java index e6e6ea96474..2a8ff81432a 100644 --- a/spring-web/src/main/java/org/springframework/http/ProblemDetail.java +++ b/spring-web/src/main/java/org/springframework/http/ProblemDetail.java @@ -209,7 +209,7 @@ public class ProblemDetail { * @param value the property value * @see org.springframework.http.converter.json.ProblemDetailJacksonMixin */ - public void setProperty(String name, Object value) { + public void setProperty(String name, @Nullable Object value) { this.properties = (this.properties != null ? this.properties : new LinkedHashMap<>()); this.properties.put(name, value); } diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/ProblemDetailJacksonMixin.java b/spring-web/src/main/java/org/springframework/http/converter/json/ProblemDetailJacksonMixin.java index abae30512b3..0fcb96b036a 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/ProblemDetailJacksonMixin.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/ProblemDetailJacksonMixin.java @@ -23,6 +23,7 @@ import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.annotation.JsonInclude; import org.springframework.http.ProblemDetail; +import org.springframework.lang.Nullable; import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY; @@ -46,7 +47,7 @@ import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY; public interface ProblemDetailJacksonMixin { @JsonAnySetter - void setProperty(String name, Object value); + void setProperty(String name, @Nullable Object value); @JsonAnyGetter Map getProperties(); diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/ProblemDetailJacksonMixinTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/ProblemDetailJacksonMixinTests.java index 8632d66e725..5058485df12 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/ProblemDetailJacksonMixinTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/ProblemDetailJacksonMixinTests.java @@ -52,13 +52,15 @@ public class ProblemDetailJacksonMixinTests { void writeCustomProperty() throws Exception { ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Missing header"); problemDetail.setProperty("host", "abc.org"); + problemDetail.setProperty("user", null); testWrite(problemDetail, "{\"type\":\"about:blank\"," + "\"title\":\"Bad Request\"," + "\"status\":400," + "\"detail\":\"Missing header\"," + - "\"host\":\"abc.org\"}"); + "\"host\":\"abc.org\"," + + "\"user\":null}"); } @Test @@ -68,13 +70,15 @@ public class ProblemDetailJacksonMixinTests { "\"title\":\"Bad Request\"," + "\"status\":400," + "\"detail\":\"Missing header\"," + - "\"host\":\"abc.org\"}", ProblemDetail.class); + "\"host\":\"abc.org\"," + + "\"user\":null}", ProblemDetail.class); assertThat(problemDetail.getType()).isEqualTo(URI.create("about:blank")); assertThat(problemDetail.getTitle()).isEqualTo("Bad Request"); assertThat(problemDetail.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value()); assertThat(problemDetail.getDetail()).isEqualTo("Missing header"); assertThat(problemDetail.getProperties()).containsEntry("host", "abc.org"); + assertThat(problemDetail.getProperties()).containsEntry("user", null); }