Skip to content

Commit 422c268

Browse files
committed
Minor follow-up to previous commit
See spring-projectsgh-23741
1 parent 34cfbe5 commit 422c268

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.http.HttpStatus;
2727
import org.springframework.lang.Nullable;
2828
import org.springframework.util.Assert;
29+
import org.springframework.util.CollectionUtils;
2930
import org.springframework.util.StringUtils;
3031

3132
/**
@@ -39,7 +40,7 @@ public class MethodNotAllowedException extends ResponseStatusException {
3940

4041
private final String method;
4142

42-
private final Set<HttpMethod> supportedMethods;
43+
private final Set<HttpMethod> httpMethods;
4344

4445

4546
public MethodNotAllowedException(HttpMethod method, Collection<HttpMethod> supportedMethods) {
@@ -53,7 +54,7 @@ public MethodNotAllowedException(String method, @Nullable Collection<HttpMethod>
5354
supportedMethods = Collections.emptySet();
5455
}
5556
this.method = method;
56-
this.supportedMethods = Collections.unmodifiableSet(new HashSet<>(supportedMethods));
57+
this.httpMethods = Collections.unmodifiableSet(new HashSet<>(supportedMethods));
5758
}
5859

5960

@@ -63,8 +64,9 @@ public MethodNotAllowedException(String method, @Nullable Collection<HttpMethod>
6364
*/
6465
@Override
6566
public Map<String, String> getHeaders() {
66-
return Collections.singletonMap("Allow",
67-
StringUtils.collectionToDelimitedString(this.supportedMethods, ", "));
67+
return !CollectionUtils.isEmpty(this.httpMethods) ?
68+
Collections.singletonMap("Allow", StringUtils.collectionToDelimitedString(this.httpMethods, ", ")) :
69+
Collections.emptyMap();
6870
}
6971

7072
/**
@@ -78,7 +80,7 @@ public String getHttpMethod() {
7880
* Return the list of supported HTTP methods.
7981
*/
8082
public Set<HttpMethod> getSupportedMethods() {
81-
return this.supportedMethods;
83+
return this.httpMethods;
8284
}
8385

8486
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.http.HttpStatus;
2424
import org.springframework.http.MediaType;
25+
import org.springframework.util.CollectionUtils;
2526

2627
/**
2728
* Exception for errors that fit response status 406 (not acceptable).
@@ -58,7 +59,9 @@ public NotAcceptableStatusException(List<MediaType> supportedMediaTypes) {
5859
*/
5960
@Override
6061
public Map<String, String> getHeaders() {
61-
return Collections.singletonMap("Accept", MediaType.toString(this.supportedMediaTypes));
62+
return !CollectionUtils.isEmpty(this.supportedMediaTypes) ?
63+
Collections.singletonMap("Accept", MediaType.toString(this.supportedMediaTypes)) :
64+
Collections.emptyMap();
6265
}
6366

6467
/**

0 commit comments

Comments
 (0)