Skip to content

Commit e707c40

Browse files
bedrinrstoyanchev
authored andcommitted
Add consumes attribute to @GetMapping
Issue: SPR-14988
1 parent 9d37472 commit e707c40

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.annotation.Target;
2323
import java.lang.reflect.Method;
2424
import java.util.Arrays;
25+
import java.util.ArrayList;
2526
import java.util.Collections;
2627
import java.util.HashSet;
2728
import java.util.Map;
@@ -145,6 +146,14 @@ public void resolveRequestMappingViaComposedAnnotation() throws Exception {
145146
info.getProducesCondition().getProducibleMediaTypes().iterator().next().toString());
146147
}
147148

149+
@Test // SPR-14988
150+
public void getMappingOverridesConsumesFromTypeLevelAnnotation() throws Exception {
151+
RequestMappingInfo requestMappingInfo = assertComposedAnnotationMapping(RequestMethod.GET);
152+
153+
assertArrayEquals(new MediaType[]{MediaType.ALL}, new ArrayList<>(
154+
requestMappingInfo.getConsumesCondition().getConsumableMediaTypes()).toArray());
155+
}
156+
148157
@Test
149158
public void getMapping() throws Exception {
150159
assertComposedAnnotationMapping(RequestMethod.GET);
@@ -199,6 +208,7 @@ private RequestMappingInfo assertComposedAnnotationMapping(String methodName, St
199208

200209

201210
@Controller
211+
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
202212
static class ComposedAnnotationController {
203213

204214
@RequestMapping
@@ -209,7 +219,7 @@ public void handle() {
209219
public void postJson() {
210220
}
211221

212-
@GetMapping("/get")
222+
@GetMapping(value = "/get", consumes = MediaType.ALL_VALUE)
213223
public void get() {
214224
}
215225

0 commit comments

Comments
 (0)