Skip to content

Commit ea6d2ea

Browse files
committed
Multi-value headers in ResponseStatusException
Closes gh-24261
1 parent f9c1565 commit ea6d2ea

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -22,12 +22,12 @@
2222
import java.util.Map;
2323
import java.util.Set;
2424

25+
import org.springframework.http.HttpHeaders;
2526
import org.springframework.http.HttpMethod;
2627
import org.springframework.http.HttpStatus;
2728
import org.springframework.lang.Nullable;
2829
import org.springframework.util.Assert;
2930
import org.springframework.util.CollectionUtils;
30-
import org.springframework.util.StringUtils;
3131

3232
/**
3333
* Exception for errors that fit response status 405 (method not allowed).
@@ -62,11 +62,24 @@ public MethodNotAllowedException(String method, @Nullable Collection<HttpMethod>
6262
* Return a Map with an "Allow" header.
6363
* @since 5.1.11
6464
*/
65+
@SuppressWarnings("deprecation")
6566
@Override
6667
public Map<String, String> getHeaders() {
67-
return !CollectionUtils.isEmpty(this.httpMethods) ?
68-
Collections.singletonMap("Allow", StringUtils.collectionToDelimitedString(this.httpMethods, ", ")) :
69-
Collections.emptyMap();
68+
return getResponseHeaders().toSingleValueMap();
69+
}
70+
71+
/**
72+
* Return HttpHeaders with an "Allow" header.
73+
* @since 5.1.13
74+
*/
75+
@Override
76+
public HttpHeaders getResponseHeaders() {
77+
if (CollectionUtils.isEmpty(this.httpMethods)) {
78+
return HttpHeaders.EMPTY;
79+
}
80+
HttpHeaders headers = new HttpHeaders();
81+
headers.setAllow(this.httpMethods);
82+
return headers;
7083
}
7184

7285
/**

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.Map;
2222

23+
import org.springframework.http.HttpHeaders;
2324
import org.springframework.http.HttpStatus;
2425
import org.springframework.http.MediaType;
2526
import org.springframework.util.CollectionUtils;
@@ -57,11 +58,24 @@ public NotAcceptableStatusException(List<MediaType> supportedMediaTypes) {
5758
* Return a Map with an "Accept" header.
5859
* @since 5.1.11
5960
*/
61+
@SuppressWarnings("deprecation")
6062
@Override
6163
public Map<String, String> getHeaders() {
62-
return !CollectionUtils.isEmpty(this.supportedMediaTypes) ?
63-
Collections.singletonMap("Accept", MediaType.toString(this.supportedMediaTypes)) :
64-
Collections.emptyMap();
64+
return getResponseHeaders().toSingleValueMap();
65+
}
66+
67+
/**
68+
* Return HttpHeaders with an "Accept" header, or an empty instance.
69+
* @since 5.1.13
70+
*/
71+
@Override
72+
public HttpHeaders getResponseHeaders() {
73+
if (CollectionUtils.isEmpty(this.supportedMediaTypes)) {
74+
return HttpHeaders.EMPTY;
75+
}
76+
HttpHeaders headers = new HttpHeaders();
77+
headers.setAccept(this.supportedMediaTypes);
78+
return headers;
6579
}
6680

6781
/**

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.core.NestedExceptionUtils;
2323
import org.springframework.core.NestedRuntimeException;
24+
import org.springframework.http.HttpHeaders;
2425
import org.springframework.http.HttpStatus;
2526
import org.springframework.lang.Nullable;
2627
import org.springframework.util.Assert;
@@ -82,14 +83,33 @@ public HttpStatus getStatus() {
8283
}
8384

8485
/**
85-
* Return response headers associated with the exception, possibly required
86-
* for the given status code (e.g. "Allow", "Accept").
86+
* Return headers associated with the exception that should be added to the
87+
* error response, e.g. "Allow", "Accept", etc.
88+
* <p>The default implementation in this class returns an empty map.
8789
* @since 5.1.11
90+
* @deprecated as of 5.1.13 in favor of {@link #getResponseHeaders()}
8891
*/
92+
@Deprecated
8993
public Map<String, String> getHeaders() {
9094
return Collections.emptyMap();
9195
}
9296

97+
/**
98+
* Return headers associated with the exception that should be added to the
99+
* error response, e.g. "Allow", "Accept", etc.
100+
* <p>The default implementation in this class returns empty headers.
101+
* @since 5.1.13
102+
*/
103+
public HttpHeaders getResponseHeaders() {
104+
Map<String, String> headers = getHeaders();
105+
if (headers.isEmpty()) {
106+
return HttpHeaders.EMPTY;
107+
}
108+
HttpHeaders result = new HttpHeaders();
109+
getHeaders().forEach(result::add);
110+
return result;
111+
}
112+
93113
/**
94114
* The reason explaining the exception (potentially {@code null} or empty).
95115
*/

spring-web/src/main/java/org/springframework/web/server/handler/ResponseStatusExceptionHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -92,8 +92,9 @@ private boolean updateResponse(ServerHttpResponse response, Throwable ex) {
9292
if (status != null) {
9393
if (response.setStatusCode(status)) {
9494
if (ex instanceof ResponseStatusException) {
95-
((ResponseStatusException) ex).getHeaders()
96-
.forEach((name, value) -> response.getHeaders().add(name, value));
95+
((ResponseStatusException) ex).getResponseHeaders()
96+
.forEach((name, values) ->
97+
values.forEach(value -> response.getHeaders().add(name, value)));
9798
}
9899
result = true;
99100
}

0 commit comments

Comments
 (0)