From c55486d5d589ca09ae4dde8dc8503810dbf980db Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 31 May 2015 16:32:45 +0200 Subject: [PATCH] Introduce alias for 'value' attribute in @RequestPart Issue: SPR-11393 --- .../web/bind/annotation/RequestPart.java | 24 ++++++++++++++----- .../RequestPartMethodArgumentResolver.java | 8 +++---- .../RequestPartIntegrationTests.java | 5 ++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestPart.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestPart.java index a299313dce..ade9e957ec 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestPart.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestPart.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.core.annotation.AliasFor; import org.springframework.core.convert.converter.Converter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.multipart.MultipartFile; @@ -30,7 +31,9 @@ import org.springframework.web.multipart.MultipartResolver; /** * Annotation that can be used to associate the part of a "multipart/form-data" request - * with a method argument. Supported method argument types include {@link MultipartFile} + * with a method argument. + * + *

Supported method argument types include {@link MultipartFile} * in conjunction with Spring's {@link MultipartResolver} abstraction, * {@code javax.servlet.http.Part} in conjunction with Servlet 3.0 multipart requests, * or otherwise for any other method argument, the content of the part is passed through an @@ -50,6 +53,7 @@ import org.springframework.web.multipart.MultipartResolver; * * @author Rossen Stoyanchev * @author Arjen Poutsma + * @author Sam Brannen * @since 3.1 * * @see RequestParam @@ -61,15 +65,23 @@ import org.springframework.web.multipart.MultipartResolver; public @interface RequestPart { /** - * The name of the part in the "multipart/form-data" request to bind to. + * Alias for {@link #name}. */ + @AliasFor(attribute = "name") String value() default ""; + /** + * The name of the part in the "multipart/form-data" request to bind to. + * @since 4.2 + */ + @AliasFor(attribute = "value") + String name() default ""; + /** * Whether the part is required. - *

Default is {@code true}, leading to an exception thrown in case - * of the part missing in the request. Switch this to {@code false} - * if you prefer a {@code null} in case of the part missing. + *

Default is {@code true}, leading to an exception being thrown + * in case the part is missing in the request. Switch this to + * {@code false} if you prefer a {@code null} if the part is missing. */ boolean required() default true; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java index 75014314eb..09068e9d2b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java @@ -188,8 +188,8 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM } } - RequestPart ann = parameter.getParameterAnnotation(RequestPart.class); - boolean isRequired = ((ann == null || ann.required()) && !optional); + RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class); + boolean isRequired = ((requestPart == null || requestPart.required()) && !optional); if (arg == null && isRequired) { throw new MissingServletRequestPartException(partName); @@ -209,8 +209,8 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM } private String getPartName(MethodParameter methodParam) { - RequestPart ann = methodParam.getParameterAnnotation(RequestPart.class); - String partName = (ann != null ? ann.value() : ""); + RequestPart requestPart = methodParam.getParameterAnnotation(RequestPart.class); + String partName = (requestPart != null ? requestPart.name() : ""); if (partName.length() == 0) { partName = methodParam.getParameterName(); if (partName == null) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartIntegrationTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartIntegrationTests.java index 0b254d0099..67ffce7aca 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartIntegrationTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartIntegrationTests.java @@ -179,14 +179,13 @@ public class RequestPartIntegrationTests { } } - @SuppressWarnings("unused") @Controller private static class RequestPartTestController { @RequestMapping(value = "/test", method = RequestMethod.POST, consumes = { "multipart/mixed", "multipart/form-data" }) - public ResponseEntity create(@RequestPart("json-data") TestData testData, + public ResponseEntity create(@RequestPart(name = "json-data") TestData testData, @RequestPart("file-data") MultipartFile file, - @RequestPart(value = "empty-data", required = false) TestData emptyData) { + @RequestPart(name = "empty-data", required = false) TestData emptyData) { String url = "http://localhost:8080/test/" + testData.getName() + "/" + file.getOriginalFilename(); HttpHeaders headers = new HttpHeaders();