diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java index 6b277f7677d..21a9d5993d0 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java @@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.method; import java.util.ArrayList; import java.util.Comparator; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -46,17 +45,6 @@ import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping; */ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMethodMapping { - @Override - protected void handlerMethodsInitialized(Map handlerMethods) { - List infos = new ArrayList(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}. */ diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java index 20a4b1c8f88..dd6e720cadf 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java @@ -33,9 +33,13 @@ import org.junit.Before; import org.junit.Test; import org.springframework.context.support.StaticApplicationContext; import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.stereotype.Controller; +import org.springframework.web.HttpMediaTypeNotAcceptableException; +import org.springframework.web.HttpMediaTypeNotSupportedException; 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.RequestMethod; import org.springframework.web.method.HandlerMethod; @@ -132,7 +136,7 @@ public class RequestMappingInfoHandlerMappingTests { } @Test - public void methodNotAllowed() throws Exception { + public void requestMethodNotAllowed() throws Exception { try { MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bar"); mapping.getHandler(request); @@ -142,6 +146,46 @@ public class RequestMappingInfoHandlerMappingTests { 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 public void uriTemplateVariables() { @@ -202,6 +246,15 @@ public class RequestMappingInfoHandlerMappingTests { @RequestMapping(value = "") 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 {