Skip to content

Commit 8315a40

Browse files
bedrinrstoyanchev
authored andcommitted
Add consumes attribute to @GetMapping
Issue: SPR-14988
1 parent 1b0e95d commit 8315a40

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMappingTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323
import java.lang.reflect.Method;
24+
import java.util.ArrayList;
2425
import java.util.Collections;
2526
import java.util.HashSet;
2627
import java.util.Set;
@@ -147,6 +148,14 @@ public void resolveRequestMappingViaComposedAnnotation() throws Exception {
147148
info.getProducesCondition().getProducibleMediaTypes().iterator().next().toString());
148149
}
149150

151+
@Test // SPR-14988
152+
public void getMappingOverridesConsumesFromTypeLevelAnnotation() throws Exception {
153+
RequestMappingInfo requestMappingInfo = assertComposedAnnotationMapping(RequestMethod.GET);
154+
155+
assertArrayEquals(new MediaType[]{MediaType.ALL}, new ArrayList<>(
156+
requestMappingInfo.getConsumesCondition().getConsumableMediaTypes()).toArray());
157+
}
158+
150159
@Test
151160
public void getMapping() throws Exception {
152161
assertComposedAnnotationMapping(RequestMethod.GET);
@@ -201,6 +210,7 @@ private RequestMappingInfo assertComposedAnnotationMapping(String methodName, St
201210

202211

203212
@Controller @SuppressWarnings("unused")
213+
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
204214
static class ComposedAnnotationController {
205215

206216
@RequestMapping
@@ -211,7 +221,7 @@ public void handle() {
211221
public void postJson() {
212222
}
213223

214-
@GetMapping("/get")
224+
@GetMapping(value = "/get", consumes = MediaType.ALL_VALUE)
215225
public void get() {
216226
}
217227

spring-web/src/main/java/org/springframework/web/bind/annotation/GetMapping.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
* <p>Specifically, {@code @GetMapping} is a <em>composed annotation</em> that
3232
* acts as a shortcut for {@code @RequestMapping(method = RequestMethod.GET)}.
3333
*
34-
* <h5>Difference between {@code @GetMapping} &amp; {@code @RequestMapping}</h5>
35-
* <p>{@code @GetMapping} does not support the {@link RequestMapping#consumes consumes}
36-
* attribute of {@code @RequestMapping}.
3734
*
3835
* @author Sam Brannen
3936
* @since 4.3
@@ -79,6 +76,13 @@
7976
@AliasFor(annotation = RequestMapping.class)
8077
String[] headers() default {};
8178

79+
/**
80+
* Alias for {@link RequestMapping#consumes}.
81+
* @since 4.3.5
82+
*/
83+
@AliasFor(annotation = RequestMapping.class)
84+
String[] consumes() default {};
85+
8286
/**
8387
* Alias for {@link RequestMapping#produces}.
8488
*/

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323
import java.lang.reflect.Method;
24+
import java.util.ArrayList;
2425
import java.util.Arrays;
2526
import java.util.Collections;
2627
import java.util.HashSet;
@@ -44,7 +45,11 @@
4445
import org.springframework.web.context.support.StaticWebApplicationContext;
4546
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
4647

47-
import static org.junit.Assert.*;
48+
import static org.junit.Assert.assertArrayEquals;
49+
import static org.junit.Assert.assertEquals;
50+
import static org.junit.Assert.assertFalse;
51+
import static org.junit.Assert.assertNotNull;
52+
import static org.junit.Assert.assertTrue;
4853

4954
/**
5055
* Tests for {@link RequestMappingHandlerMapping}.
@@ -145,6 +150,14 @@ public void resolveRequestMappingViaComposedAnnotation() throws Exception {
145150
info.getProducesCondition().getProducibleMediaTypes().iterator().next().toString());
146151
}
147152

153+
@Test // SPR-14988
154+
public void getMappingOverridesConsumesFromTypeLevelAnnotation() throws Exception {
155+
RequestMappingInfo requestMappingInfo = assertComposedAnnotationMapping(RequestMethod.GET);
156+
157+
assertArrayEquals(new MediaType[]{MediaType.ALL}, new ArrayList<>(
158+
requestMappingInfo.getConsumesCondition().getConsumableMediaTypes()).toArray());
159+
}
160+
148161
@Test
149162
public void getMapping() throws Exception {
150163
assertComposedAnnotationMapping(RequestMethod.GET);
@@ -199,6 +212,7 @@ private RequestMappingInfo assertComposedAnnotationMapping(String methodName, St
199212

200213

201214
@Controller
215+
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
202216
static class ComposedAnnotationController {
203217

204218
@RequestMapping
@@ -209,7 +223,7 @@ public void handle() {
209223
public void postJson() {
210224
}
211225

212-
@GetMapping("/get")
226+
@GetMapping(value = "/get", consumes = MediaType.ALL_VALUE)
213227
public void get() {
214228
}
215229

0 commit comments

Comments
 (0)