SPR-8462
This commit is contained in:
parent
4fb811572e
commit
c41caa3c2f
|
|
@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
|
@ -46,17 +45,6 @@ import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;
|
||||||
*/
|
*/
|
||||||
public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMethodMapping<RequestMappingInfo> {
|
public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMethodMapping<RequestMappingInfo> {
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void handlerMethodsInitialized(Map<RequestMappingInfo, HandlerMethod> handlerMethods) {
|
|
||||||
List<RequestMappingInfo> infos = new ArrayList<RequestMappingInfo>(handlerMethods.keySet());
|
|
||||||
while (infos.size() > 1) {
|
|
||||||
RequestMappingInfo info1 = infos.remove(0);
|
|
||||||
for (RequestMappingInfo info2 : infos) {
|
|
||||||
// TODO: validate duplicate consumable and producible media types
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the URL path patterns associated with this {@link RequestMappingInfo}.
|
* Get the URL path patterns associated with this {@link RequestMappingInfo}.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -33,9 +33,13 @@ import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.context.support.StaticApplicationContext;
|
import org.springframework.context.support.StaticApplicationContext;
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.mock.web.MockHttpServletRequest;
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
||||||
|
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.method.HandlerMethod;
|
import org.springframework.web.method.HandlerMethod;
|
||||||
|
|
@ -132,7 +136,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void methodNotAllowed() throws Exception {
|
public void requestMethodNotAllowed() throws Exception {
|
||||||
try {
|
try {
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bar");
|
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bar");
|
||||||
mapping.getHandler(request);
|
mapping.getHandler(request);
|
||||||
|
|
@ -142,6 +146,46 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||||
assertArrayEquals("Invalid supported methods", new String[]{"GET", "HEAD"}, ex.getSupportedMethods());
|
assertArrayEquals("Invalid supported methods", new String[]{"GET", "HEAD"}, ex.getSupportedMethods());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mediaTypeNotSupported() throws Exception {
|
||||||
|
testMediaTypeNotSupported("/person/1");
|
||||||
|
testMediaTypeNotSupported("/person/1/"); // SPR-8462
|
||||||
|
testMediaTypeNotSupported("/person/1.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testMediaTypeNotSupported(String url) throws Exception {
|
||||||
|
try {
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest("PUT", url);
|
||||||
|
request.setContentType("application/json");
|
||||||
|
mapping.getHandler(request);
|
||||||
|
fail("HttpMediaTypeNotSupportedException expected");
|
||||||
|
}
|
||||||
|
catch (HttpMediaTypeNotSupportedException ex) {
|
||||||
|
assertEquals("Invalid supported consumable media types",
|
||||||
|
Arrays.asList(new MediaType("application", "xml")), ex.getSupportedMediaTypes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mediaTypeNotAccepted() throws Exception {
|
||||||
|
testMediaTypeNotAccepted("/persons");
|
||||||
|
testMediaTypeNotAccepted("/persons/"); // SPR-8462
|
||||||
|
testMediaTypeNotAccepted("/persons.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testMediaTypeNotAccepted(String url) throws Exception {
|
||||||
|
try {
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", url);
|
||||||
|
request.addHeader("Accept", "application/json");
|
||||||
|
mapping.getHandler(request);
|
||||||
|
fail("HttpMediaTypeNotAcceptableException expected");
|
||||||
|
}
|
||||||
|
catch (HttpMediaTypeNotAcceptableException ex) {
|
||||||
|
assertEquals("Invalid supported producible media types",
|
||||||
|
Arrays.asList(new MediaType("application", "xml")), ex.getSupportedMediaTypes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void uriTemplateVariables() {
|
public void uriTemplateVariables() {
|
||||||
|
|
@ -202,6 +246,15 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||||
@RequestMapping(value = "")
|
@RequestMapping(value = "")
|
||||||
public void empty() {
|
public void empty() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/person/{id}", method = RequestMethod.PUT, consumes="application/xml")
|
||||||
|
public void consumes(@RequestBody String text) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/persons", produces="application/xml")
|
||||||
|
public String produces() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class TestRequestMappingInfoHandlerMapping extends RequestMappingInfoHandlerMapping {
|
private static class TestRequestMappingInfoHandlerMapping extends RequestMappingInfoHandlerMapping {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue