Change MethodNotAllowedException to use HttpMethod

Changed the MethodNotAllowedException to use HttpMethod, instead of
Strings.
This commit is contained in:
Arjen Poutsma
2017-03-22 16:38:26 +01:00
parent 4db2daae5c
commit 290e9bea14
5 changed files with 53 additions and 58 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
@@ -33,14 +34,21 @@ import org.springframework.util.Assert;
@SuppressWarnings("serial")
public class MethodNotAllowedException extends ResponseStatusException {
private String method;
private final String method;
private Set<String> supportedMethods;
private final Set<HttpMethod> supportedMethods;
public MethodNotAllowedException(String method, Collection<String> supportedMethods) {
public MethodNotAllowedException(HttpMethod method, Collection<HttpMethod> supportedMethods) {
this(method.name(), supportedMethods);
}
public MethodNotAllowedException(String method, Collection<HttpMethod> supportedMethods) {
super(HttpStatus.METHOD_NOT_ALLOWED, "Request method '" + method + "' not supported");
Assert.notNull(method, "'method' is required");
if (supportedMethods == null) {
supportedMethods = Collections.emptySet();
}
this.method = method;
this.supportedMethods = Collections.unmodifiableSet(new HashSet<>(supportedMethods));
}
@@ -56,7 +64,7 @@ public class MethodNotAllowedException extends ResponseStatusException {
/**
* Return the list of supported HTTP methods.
*/
public Set<String> getSupportedMethods() {
public Set<HttpMethod> getSupportedMethods() {
return supportedMethods;
}
}