Browse Source

Minor follow-up to previous commit

See gh-23741
pull/27800/head
Rossen Stoyanchev 5 years ago
parent
commit
422c26832b
  1. 12
      spring-web/src/main/java/org/springframework/web/server/MethodNotAllowedException.java
  2. 5
      spring-web/src/main/java/org/springframework/web/server/NotAcceptableStatusException.java

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

@ -26,6 +26,7 @@ import org.springframework.http.HttpMethod; @@ -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 { @@ -39,7 +40,7 @@ public class MethodNotAllowedException extends ResponseStatusException {
private final String method;
private final Set<HttpMethod> supportedMethods;
private final Set<HttpMethod> httpMethods;
public MethodNotAllowedException(HttpMethod method, Collection<HttpMethod> supportedMethods) {
@ -53,7 +54,7 @@ public class MethodNotAllowedException extends ResponseStatusException { @@ -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 { @@ -63,8 +64,9 @@ public class MethodNotAllowedException extends ResponseStatusException {
*/
@Override
public Map<String, String> 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 { @@ -78,7 +80,7 @@ public class MethodNotAllowedException extends ResponseStatusException {
* Return the list of supported HTTP methods.
*/
public Set<HttpMethod> getSupportedMethods() {
return this.supportedMethods;
return this.httpMethods;
}
}

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

@ -22,6 +22,7 @@ import java.util.Map; @@ -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 { @@ -58,7 +59,9 @@ public class NotAcceptableStatusException extends ResponseStatusException {
*/
@Override
public Map<String, String> getHeaders() {
return Collections.singletonMap("Accept", MediaType.toString(this.supportedMediaTypes));
return !CollectionUtils.isEmpty(this.supportedMediaTypes) ?
Collections.singletonMap("Accept", MediaType.toString(this.supportedMediaTypes)) :
Collections.emptyMap();
}
/**

Loading…
Cancel
Save