Add consumes attribute to @GetMapping

Issue: SPR-14988
This commit is contained in:
bedrin 2016-12-08 01:29:48 +03:00 committed by Rossen Stoyanchev
parent 1b0e95d7d8
commit 8315a4033f
3 changed files with 34 additions and 6 deletions

View File

@ -21,6 +21,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@ -147,6 +148,14 @@ public class RequestMappingHandlerMappingTests {
info.getProducesCondition().getProducibleMediaTypes().iterator().next().toString());
}
@Test // SPR-14988
public void getMappingOverridesConsumesFromTypeLevelAnnotation() throws Exception {
RequestMappingInfo requestMappingInfo = assertComposedAnnotationMapping(RequestMethod.GET);
assertArrayEquals(new MediaType[]{MediaType.ALL}, new ArrayList<>(
requestMappingInfo.getConsumesCondition().getConsumableMediaTypes()).toArray());
}
@Test
public void getMapping() throws Exception {
assertComposedAnnotationMapping(RequestMethod.GET);
@ -201,6 +210,7 @@ public class RequestMappingHandlerMappingTests {
@Controller @SuppressWarnings("unused")
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
static class ComposedAnnotationController {
@RequestMapping
@ -211,7 +221,7 @@ public class RequestMappingHandlerMappingTests {
public void postJson() {
}
@GetMapping("/get")
@GetMapping(value = "/get", consumes = MediaType.ALL_VALUE)
public void get() {
}

View File

@ -31,9 +31,6 @@ import org.springframework.core.annotation.AliasFor;
* <p>Specifically, {@code @GetMapping} is a <em>composed annotation</em> that
* acts as a shortcut for {@code @RequestMapping(method = RequestMethod.GET)}.
*
* <h5>Difference between {@code @GetMapping} &amp; {@code @RequestMapping}</h5>
* <p>{@code @GetMapping} does not support the {@link RequestMapping#consumes consumes}
* attribute of {@code @RequestMapping}.
*
* @author Sam Brannen
* @since 4.3
@ -79,6 +76,13 @@ public @interface GetMapping {
@AliasFor(annotation = RequestMapping.class)
String[] headers() default {};
/**
* Alias for {@link RequestMapping#consumes}.
* @since 4.3.5
*/
@AliasFor(annotation = RequestMapping.class)
String[] consumes() default {};
/**
* Alias for {@link RequestMapping#produces}.
*/

View File

@ -21,6 +21,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
@ -44,7 +45,11 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link RequestMappingHandlerMapping}.
@ -145,6 +150,14 @@ public class RequestMappingHandlerMappingTests {
info.getProducesCondition().getProducibleMediaTypes().iterator().next().toString());
}
@Test // SPR-14988
public void getMappingOverridesConsumesFromTypeLevelAnnotation() throws Exception {
RequestMappingInfo requestMappingInfo = assertComposedAnnotationMapping(RequestMethod.GET);
assertArrayEquals(new MediaType[]{MediaType.ALL}, new ArrayList<>(
requestMappingInfo.getConsumesCondition().getConsumableMediaTypes()).toArray());
}
@Test
public void getMapping() throws Exception {
assertComposedAnnotationMapping(RequestMethod.GET);
@ -199,6 +212,7 @@ public class RequestMappingHandlerMappingTests {
@Controller
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
static class ComposedAnnotationController {
@RequestMapping
@ -209,7 +223,7 @@ public class RequestMappingHandlerMappingTests {
public void postJson() {
}
@GetMapping("/get")
@GetMapping(value = "/get", consumes = MediaType.ALL_VALUE)
public void get() {
}