Consistent and lenient HttpMethod resolution across all web modules

Issue: SPR-13776
This commit is contained in:
Juergen Hoeller
2015-12-09 12:26:44 +01:00
parent 34b596c6bf
commit 4261f34b63
29 changed files with 149 additions and 109 deletions

View File

@@ -1137,9 +1137,8 @@ public class DispatcherServlet extends FrameworkServlet {
"] in DispatcherServlet with name '" + getServletName() + "'");
}
if (this.throwExceptionIfNoHandlerFound) {
ServletServerHttpRequest sshr = new ServletServerHttpRequest(request);
throw new NoHandlerFoundException(
sshr.getMethod().name(), sshr.getServletRequest().getRequestURI(), sshr.getHeaders());
throw new NoHandlerFoundException(request.getMethod(), getRequestUri(request),
new ServletServerHttpRequest(request).getHeaders());
}
else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);

View File

@@ -42,6 +42,7 @@ import org.springframework.context.i18n.SimpleLocaleContext;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
@@ -831,15 +832,13 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
/**
* Override the parent class implementation in order to intercept PATCH
* requests.
* Override the parent class implementation in order to intercept PATCH requests.
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getMethod();
if (method.equalsIgnoreCase(RequestMethod.PATCH.name())) {
if (HttpMethod.PATCH.matches(request.getMethod())) {
processRequest(request, response);
}
else {

View File

@@ -22,8 +22,8 @@ import java.io.PushbackInputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -63,8 +63,8 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver;
*/
public abstract class AbstractMessageConverterMethodArgumentResolver implements HandlerMethodArgumentResolver {
private static final List<HttpMethod> SUPPORTED_METHODS =
Arrays.asList(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH);
private static final Set<HttpMethod> SUPPORTED_METHODS =
EnumSet.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH);
private static final Object NO_VALUE = new Object();
@@ -228,8 +228,8 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
}
if (body == NO_VALUE) {
if (!SUPPORTED_METHODS.contains(httpMethod)
|| (noContentType && inputMessage.getBody() == null)) {
if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) ||
(noContentType && inputMessage.getBody() == null)) {
return null;
}
throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);

View File

@@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.method.annotation;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.List;
import org.springframework.core.MethodParameter;
@@ -122,9 +121,8 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
Object body = readWithMessageConverters(webRequest, parameter, paramType);
if (RequestEntity.class == parameter.getParameterType()) {
URI url = inputMessage.getURI();
HttpMethod httpMethod = inputMessage.getMethod();
return new RequestEntity<Object>(body, inputMessage.getHeaders(), httpMethod, url);
return new RequestEntity<Object>(body, inputMessage.getHeaders(),
inputMessage.getMethod(), inputMessage.getURI());
}
else {
return new HttpEntity<Object>(body, inputMessage.getHeaders());
@@ -172,8 +170,7 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
Object body = responseEntity.getBody();
if (responseEntity instanceof ResponseEntity) {
outputMessage.setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
if (inputMessage.getMethod().equals(HttpMethod.GET) &&
isResourceNotModified(inputMessage, outputMessage)) {
if (HttpMethod.GET == inputMessage.getMethod() && isResourceNotModified(inputMessage, outputMessage)) {
outputMessage.setStatusCode(HttpStatus.NOT_MODIFIED);
// Ensure headers are flushed, no body should be written.
outputMessage.flush();