Add generic properties map to ProblemDetail

Closes gh-28665
This commit is contained in:
rstoyanchev 2022-06-24 10:42:20 +01:00
parent b72ee5f034
commit c139f3d526
1 changed files with 28 additions and 1 deletions

View File

@ -17,6 +17,8 @@
package org.springframework.http; package org.springframework.http;
import java.net.URI; import java.net.URI;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -54,6 +56,9 @@ public class ProblemDetail {
@Nullable @Nullable
private URI instance; private URI instance;
@Nullable
private Map<String, Object> properties;
/** /**
* Protected constructor for subclasses. * Protected constructor for subclasses.
@ -75,6 +80,7 @@ public class ProblemDetail {
this.status = other.status; this.status = other.status;
this.detail = other.detail; this.detail = other.detail;
this.instance = other.instance; this.instance = other.instance;
this.properties = (other.properties != null ? new LinkedHashMap<>(other.properties) : null);
} }
/** /**
@ -201,6 +207,18 @@ public class ProblemDetail {
this.instance = instance; this.instance = instance;
} }
/**
* Set a "dynamic" property to be added to a generic {@link #getProperties()
* properties map}.
* @param name the property name
* @param value the property value
*/
public void setProperty(String name, Object value) {
this.properties = (this.properties != null ? this.properties : new LinkedHashMap<>());
this.properties.put(name, value);
}
// Getters // Getters
@ -249,6 +267,14 @@ public class ProblemDetail {
return this.instance; return this.instance;
} }
/**
* Return a generic map of properties that are not known ahead of time.
*/
@Nullable
public Map<String, Object> getProperties() {
return this.properties;
}
@Override @Override
public String toString() { public String toString() {
@ -264,7 +290,8 @@ public class ProblemDetail {
", title='" + getTitle() + "'" + ", title='" + getTitle() + "'" +
", status=" + getStatus() + ", status=" + getStatus() +
", detail='" + getDetail() + "'" + ", detail='" + getDetail() + "'" +
", instance='" + getInstance() + "'"; ", instance='" + getInstance() + "'" +
", properties='" + getProperties() + "'";
} }