Skip to content

Commit 98c7d81

Browse files
committed
Remove "with" methods in ProblemDetail
ProblemDetail is intended to be extended with additional fields. This commit removes its "with" methods for chained initialization to keep it as plain as possible and avoid imposing a particular style on subclasses. See gh-27052
1 parent c139f3d commit 98c7d81

File tree

7 files changed

+25
-79
lines changed

7 files changed

+25
-79
lines changed

spring-web/src/main/java/org/springframework/http/ProblemDetail.java

Lines changed: 18 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -90,75 +90,10 @@ protected ProblemDetail() {
9090
}
9191

9292

93-
/**
94-
* Variant of {@link #setType(URI)} for chained initialization.
95-
* @param type the problem type
96-
* @return the same instance
97-
*/
98-
public ProblemDetail withType(URI type) {
99-
setType(type);
100-
return this;
101-
}
102-
103-
/**
104-
* Variant of {@link #setTitle(String)} for chained initialization.
105-
* @param title the problem title
106-
* @return the same instance
107-
*/
108-
public ProblemDetail withTitle(@Nullable String title) {
109-
setTitle(title);
110-
return this;
111-
}
112-
113-
/**
114-
* Variant of {@link #setStatus(int)} for chained initialization.
115-
* @param statusCode the response status for the problem
116-
* @return the same instance
117-
*/
118-
public ProblemDetail withStatus(HttpStatusCode statusCode) {
119-
Assert.notNull(statusCode, "HttpStatus is required");
120-
setStatus(statusCode.value());
121-
return this;
122-
}
123-
124-
/**
125-
* Variant of {@link #setStatus(int)} for chained initialization.
126-
* @param status the response status value for the problem
127-
* @return the same instance
128-
*/
129-
public ProblemDetail withStatus(int status) {
130-
setStatus(status);
131-
return this;
132-
}
133-
134-
/**
135-
* Variant of {@link #setDetail(String)} for chained initialization.
136-
* @param detail the problem detail
137-
* @return the same instance
138-
*/
139-
public ProblemDetail withDetail(@Nullable String detail) {
140-
setDetail(detail);
141-
return this;
142-
}
143-
144-
/**
145-
* Variant of {@link #setInstance(URI)} for chained initialization.
146-
* @param instance the problem instance URI
147-
* @return the same instance
148-
*/
149-
public ProblemDetail withInstance(@Nullable URI instance) {
150-
setInstance(instance);
151-
return this;
152-
}
153-
154-
155-
// Setters for deserialization
156-
15793
/**
15894
* Setter for the {@link #getType() problem type}.
15995
* <p>By default, this is {@link #BLANK_TYPE}.
16096
* @param type the problem type
161-
* @see #withType(URI)
16297
*/
16398
public void setType(URI type) {
16499
Assert.notNull(type, "'type' is required");
@@ -170,17 +105,22 @@ public void setType(URI type) {
170105
* <p>By default, if not explicitly set and the status is well-known, this
171106
* is sourced from the {@link HttpStatus#getReasonPhrase()}.
172107
* @param title the problem title
173-
* @see #withTitle(String)
174108
*/
175109
public void setTitle(@Nullable String title) {
176110
this.title = title;
177111
}
178112

113+
/**
114+
* Setter for the {@link #getStatus() problem status}.
115+
* @param httpStatus the problem status
116+
*/
117+
public void setStatus(HttpStatus httpStatus) {
118+
this.status = httpStatus.value();
119+
}
120+
179121
/**
180122
* Setter for the {@link #getStatus() problem status}.
181123
* @param status the problem status
182-
* @see #withStatus(HttpStatusCode)
183-
* @see #withStatus(int)
184124
*/
185125
public void setStatus(int status) {
186126
this.status = status;
@@ -190,7 +130,6 @@ public void setStatus(int status) {
190130
* Setter for the {@link #getDetail() problem detail}.
191131
* <p>By default, this is not set.
192132
* @param detail the problem detail
193-
* @see #withDetail(String)
194133
*/
195134
public void setDetail(@Nullable String detail) {
196135
this.detail = detail;
@@ -201,7 +140,6 @@ public void setDetail(@Nullable String detail) {
201140
* <p>By default, when {@code ProblemDetail} is returned from an
202141
* {@code @ExceptionHandler} method, this is initialized to the request path.
203142
* @param instance the problem instance
204-
* @see #withInstance(URI)
205143
*/
206144
public void setInstance(@Nullable URI instance) {
207145
this.instance = instance;
@@ -219,9 +157,6 @@ public void setProperty(String name, Object value) {
219157
}
220158

221159

222-
223-
// Getters
224-
225160
/**
226161
* Return the configured {@link #setType(URI) problem type}.
227162
*/
@@ -312,4 +247,14 @@ public static ProblemDetail forStatus(int status) {
312247
return new ProblemDetail(status);
313248
}
314249

250+
/**
251+
* Create a {@code ProblemDetail} instance with the given status and detail.
252+
*/
253+
public static ProblemDetail forStatusAndDetail(HttpStatusCode status, String detail) {
254+
Assert.notNull(status, "HttpStatusCode is required");
255+
ProblemDetail problemDetail = forStatus(status.value());
256+
problemDetail.setDetail(detail);
257+
return problemDetail;
258+
}
259+
315260
}

spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public HttpRequestMethodNotSupportedException(String method, @Nullable String[]
9696
this.supportedMethods = supportedMethods;
9797

9898
String detail = "Method '" + method + "' is not supported.";
99-
this.body = ProblemDetail.forStatus(getStatusCode()).withDetail(detail);
99+
this.body = ProblemDetail.forStatusAndDetail(getStatusCode(), detail);
100100
}
101101

102102

spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class MethodArgumentNotValidException extends BindException implements Er
4949
public MethodArgumentNotValidException(MethodParameter parameter, BindingResult bindingResult) {
5050
super(bindingResult);
5151
this.parameter = parameter;
52-
this.body = ProblemDetail.forStatus(getStatusCode()).withDetail("Invalid request content.");
52+
this.body = ProblemDetail.forStatusAndDetail(getStatusCode(), "Invalid request content.");
5353
}
5454

5555
@Override

spring-web/src/main/java/org/springframework/web/server/MissingRequestValueException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public MissingRequestValueException(String name, Class<?> type, String label, Me
4040
this.name = name;
4141
this.type = type;
4242
this.label = label;
43-
getBody().withDetail(getReason());
43+
getBody().setDetail(getReason());
4444
}
4545

4646

spring-web/src/main/java/org/springframework/web/server/UnsatisfiedRequestParameterException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public UnsatisfiedRequestParameterException(
4242
super(initReason(conditions, requestParams));
4343
this.conditions = conditions;
4444
this.requestParams = requestParams;
45-
getBody().withDetail("Invalid request parameters.");
45+
getBody().setDetail("Invalid request parameters.");
4646
}
4747

4848
private static String initReason(List<String> conditions, MultiValueMap<String, String> queryParams) {

spring-webmvc/src/main/java/org/springframework/web/servlet/NoHandlerFoundException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public NoHandlerFoundException(String httpMethod, String requestURL, HttpHeaders
6060
this.httpMethod = httpMethod;
6161
this.requestURL = requestURL;
6262
this.headers = headers;
63-
this.body = ProblemDetail.forStatus(getStatusCode()).withDetail(getMessage());
63+
this.body = ProblemDetail.forStatusAndDetail(getStatusCode(), getMessage());
6464
}
6565

6666
@Override

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ private void testProblemDetailMediaType(String expectedContentType) throws Excep
434434
"\"title\":\"Bad Request\"," +
435435
"\"status\":400," +
436436
"\"detail\":null," +
437-
"\"instance\":\"/path\"}");
437+
"\"instance\":\"/path\"," +
438+
"\"properties\":null}");
438439
}
439440

440441
@Test // SPR-13135

0 commit comments

Comments
 (0)