Minor follow-up to previous commit

See gh-23741
This commit is contained in:
Rossen Stoyanchev 2019-10-30 12:38:26 +00:00
parent 34cfbe5d26
commit 422c26832b
2 changed files with 11 additions and 6 deletions

View File

@ -26,6 +26,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -39,7 +40,7 @@ public class MethodNotAllowedException extends ResponseStatusException {
private final String method; private final String method;
private final Set<HttpMethod> supportedMethods; private final Set<HttpMethod> httpMethods;
public MethodNotAllowedException(HttpMethod method, Collection<HttpMethod> supportedMethods) { public MethodNotAllowedException(HttpMethod method, Collection<HttpMethod> supportedMethods) {
@ -53,7 +54,7 @@ public class MethodNotAllowedException extends ResponseStatusException {
supportedMethods = Collections.emptySet(); supportedMethods = Collections.emptySet();
} }
this.method = method; 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 @Override
public Map<String, String> getHeaders() { public Map<String, String> getHeaders() {
return Collections.singletonMap("Allow", return !CollectionUtils.isEmpty(this.httpMethods) ?
StringUtils.collectionToDelimitedString(this.supportedMethods, ", ")); 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. * Return the list of supported HTTP methods.
*/ */
public Set<HttpMethod> getSupportedMethods() { public Set<HttpMethod> getSupportedMethods() {
return this.supportedMethods; return this.httpMethods;
} }
} }

View File

@ -22,6 +22,7 @@ import java.util.Map;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
/** /**
* Exception for errors that fit response status 406 (not acceptable). * Exception for errors that fit response status 406 (not acceptable).
@ -58,7 +59,9 @@ public class NotAcceptableStatusException extends ResponseStatusException {
*/ */
@Override @Override
public Map<String, String> getHeaders() { 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();
} }
/** /**