Skip to content

Commit bbbc95f

Browse files
TAKETODAYsnicoll
authored andcommitted
Implement DefaultErrorResponseBuilder#headers(Consumer)
See gh-33156
1 parent 83f7996 commit bbbc95f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
* Default implementation of {@link ErrorResponse.Builder}.
3131
*
3232
* @author Rossen Stoyanchev
33+
* @author 海子 Yang
3334
* @since 6.0
3435
*/
3536
final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
@@ -66,18 +67,25 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
6667

6768
@Override
6869
public ErrorResponse.Builder header(String headerName, String... headerValues) {
69-
this.headers = (this.headers != null ? this.headers : new HttpHeaders());
7070
for (String headerValue : headerValues) {
71-
this.headers.add(headerName, headerValue);
71+
httpHeaders().add(headerName, headerValue);
7272
}
7373
return this;
7474
}
7575

7676
@Override
7777
public ErrorResponse.Builder headers(Consumer<HttpHeaders> headersConsumer) {
78+
headersConsumer.accept(httpHeaders());
7879
return this;
7980
}
8081

82+
private HttpHeaders httpHeaders() {
83+
if (this.headers == null) {
84+
this.headers = new HttpHeaders();
85+
}
86+
return this.headers;
87+
}
88+
8189
@Override
8290
public ErrorResponse.Builder type(URI type) {
8391
this.problemDetail.setType(type);

0 commit comments

Comments
 (0)