diff --git a/spring-web/src/main/java/org/springframework/web/server/MethodNotAllowedException.java b/spring-web/src/main/java/org/springframework/web/server/MethodNotAllowedException.java index adee5ff24b..14dcd2a62b 100644 --- a/spring-web/src/main/java/org/springframework/web/server/MethodNotAllowedException.java +++ b/spring-web/src/main/java/org/springframework/web/server/MethodNotAllowedException.java @@ -26,6 +26,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** @@ -39,7 +40,7 @@ public class MethodNotAllowedException extends ResponseStatusException { private final String method; - private final Set supportedMethods; + private final Set httpMethods; public MethodNotAllowedException(HttpMethod method, Collection supportedMethods) { @@ -53,7 +54,7 @@ public class MethodNotAllowedException extends ResponseStatusException { supportedMethods = Collections.emptySet(); } this.method = method; - this.supportedMethods = Collections.unmodifiableSet(new HashSet<>(supportedMethods)); + this.httpMethods = Collections.unmodifiableSet(new HashSet<>(supportedMethods)); } @@ -63,8 +64,9 @@ public class MethodNotAllowedException extends ResponseStatusException { */ @Override public Map getHeaders() { - return Collections.singletonMap("Allow", - StringUtils.collectionToDelimitedString(this.supportedMethods, ", ")); + return !CollectionUtils.isEmpty(this.httpMethods) ? + Collections.singletonMap("Allow", StringUtils.collectionToDelimitedString(this.httpMethods, ", ")) : + Collections.emptyMap(); } /** @@ -78,7 +80,7 @@ public class MethodNotAllowedException extends ResponseStatusException { * Return the list of supported HTTP methods. */ public Set getSupportedMethods() { - return this.supportedMethods; + return this.httpMethods; } } diff --git a/spring-web/src/main/java/org/springframework/web/server/NotAcceptableStatusException.java b/spring-web/src/main/java/org/springframework/web/server/NotAcceptableStatusException.java index 67d1f47b92..aa8a01d46a 100644 --- a/spring-web/src/main/java/org/springframework/web/server/NotAcceptableStatusException.java +++ b/spring-web/src/main/java/org/springframework/web/server/NotAcceptableStatusException.java @@ -22,6 +22,7 @@ import java.util.Map; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.util.CollectionUtils; /** * Exception for errors that fit response status 406 (not acceptable). @@ -58,7 +59,9 @@ public class NotAcceptableStatusException extends ResponseStatusException { */ @Override public Map getHeaders() { - return Collections.singletonMap("Accept", MediaType.toString(this.supportedMediaTypes)); + return !CollectionUtils.isEmpty(this.supportedMediaTypes) ? + Collections.singletonMap("Accept", MediaType.toString(this.supportedMediaTypes)) : + Collections.emptyMap(); } /**