diff --git a/spring-web/src/main/java/org/springframework/http/RequestEntity.java b/spring-web/src/main/java/org/springframework/http/RequestEntity.java index 592508b74f..d8c5189c5d 100644 --- a/spring-web/src/main/java/org/springframework/http/RequestEntity.java +++ b/spring-web/src/main/java/org/springframework/http/RequestEntity.java @@ -21,6 +21,7 @@ import java.nio.charset.Charset; import java.util.Arrays; import java.util.Map; +import org.springframework.util.Assert; import org.springframework.util.MultiValueMap; import org.springframework.util.ObjectUtils; import org.springframework.web.util.UriTemplate; @@ -28,7 +29,7 @@ import org.springframework.web.util.UriTemplate; /** * Extension of {@link HttpEntity} that adds a {@linkplain HttpMethod method} and * {@linkplain URI uri}. - * Used in {@code RestTemplate} as well {@code @Controller} methods. + * Used in {@code RestTemplate} and {@code @Controller} methods. * *

In {@code RestTemplate}, this class is used as parameter in * {@link org.springframework.web.client.RestTemplate#exchange(RequestEntity, Class) exchange()}: @@ -59,63 +60,58 @@ public class RequestEntity extends HttpEntity { private final URI url; + /** - * Create a new {@code RequestEntity} with the given method and URL, and no body nor headers. + * Constructor with method and URL but without body nor headers. * @param method the method * @param url the URL */ public RequestEntity(HttpMethod method, URI url) { - super(); - this.method = method; - this.url = url; + this(null, null, method, url); } /** - * Create a new {@code RequestEntity} with the given method, URL, body, and no headers. + * Constructor with method, URL and body but without headers. * @param body the body * @param method the method * @param url the URL */ public RequestEntity(T body, HttpMethod method, URI url) { - super(body); - this.method = method; - this.url = url; + this(body, null, method, url); } /** - * Create a new {@code RequestEntity} with the given method, URL, body, headers and no - * body + * Constructor with method, URL and headers but without body. * @param headers the headers * @param method the method * @param url the URL */ public RequestEntity(MultiValueMap headers, HttpMethod method, URI url) { - super(headers); - this.method = method; - this.url = url; + this(null, headers, method, url); } /** - * Create a new {@code RequestEntity} with the given method, URL, body, headers and - * body + * Constructor with method, URL, headers and body. * @param body the body * @param headers the headers * @param method the method * @param url the URL */ - public RequestEntity(T body, MultiValueMap headers, - HttpMethod method, URI url) { + public RequestEntity(T body, MultiValueMap headers, HttpMethod method, URI url) { super(body, headers); + Assert.notNull(method, "'method' is required"); + Assert.notNull(url, "'url' is required"); this.method = method; this.url = url; } + /** * Return the HTTP method of the request. * @return the HTTP method as an {@code HttpMethod} enum value */ public HttpMethod getMethod() { - return method; + return this.method; } /** @@ -123,9 +119,10 @@ public class RequestEntity extends HttpEntity { * @return the URL as a {@code URI} */ public URI getUrl() { - return url; + return this.url; } + @Override public boolean equals(Object other) { if (this == other) { @@ -150,7 +147,7 @@ public class RequestEntity extends HttpEntity { @Override public String toString() { StringBuilder builder = new StringBuilder("<"); - builder.append(this.method.toString()); + builder.append(this.method); builder.append(' '); builder.append(this.url); builder.append(','); @@ -172,35 +169,33 @@ public class RequestEntity extends HttpEntity { // Static builder methods /** - * Creates a builder with the given method, url, and uri variables. + * Create a builder with the given method, url, and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param method the HTTP method (GET, POST, etc) * @param url the URL * @param uriVariables the variables to expand in the template * @return the created builder */ - public static BodyBuilder method(HttpMethod method, String url, - Object... uriVariables) { + public static BodyBuilder method(HttpMethod method, String url, Object... uriVariables) { URI expanded = new UriTemplate(url).expand(uriVariables); return new DefaultBodyBuilder(method, expanded); } /** - * Creates a builder with the given method, url, and uri variables. + * Create a builder with the given method, url, and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param method the HTTP method (GET, POST, etc) * @param url the URL * @param uriVariables the variables to expand in the template * @return the created builder */ - public static BodyBuilder method(HttpMethod method, String url, - Map uriVariables) { + public static BodyBuilder method(HttpMethod method, String url, Map uriVariables) { URI expanded = new UriTemplate(url).expand(uriVariables); return new DefaultBodyBuilder(method, expanded); } /** - * Creates a builder with the given method, url, and uri variables. + * Create a builder with the given method and url. * @param method the HTTP method (GET, POST, etc) * @param url the URL * @return the created builder @@ -210,7 +205,7 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a GET builder with the given url and uri variables. + * Create a GET builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template @@ -221,7 +216,7 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a GET builder with the given url and uri variables. + * Create an HTTP GET builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template @@ -232,7 +227,7 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a GET builder with the given url. + * Create an HTTP GET builder with the given url. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @return the created builder @@ -242,7 +237,7 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a HEAD builder with the given url and uri variables. + * Create an HTTP HEAD builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template @@ -253,7 +248,7 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a HEAD builder with the given url and uri variables. + * Create an HTTP HEAD builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template @@ -264,7 +259,7 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a HEAD builder with the given url. + * Create an HTTP HEAD builder with the given url. * @param url the URL * @return the created builder */ @@ -273,7 +268,7 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a POST builder with the given url and uri variables. + * Create an HTTP POST builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template @@ -284,7 +279,7 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a POST builder with the given url and uri variables. + * Create an HTTP POST builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template @@ -295,7 +290,7 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a POST builder with the given url. + * Create an HTTP POST builder with the given url. * @param url the URL * @return the created builder */ @@ -304,31 +299,29 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a PUT builder with the given url and uri variables. + * Create an HTTP PUT builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template * @return the created builder */ - public static BodyBuilder put(String url, - Object... uriVariables) { + public static BodyBuilder put(String url, Object... uriVariables) { return method(HttpMethod.PUT, url, uriVariables); } /** - * Creates a PUT builder with the given url and uri variables. + * Create an HTTP PUT builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template * @return the created builder */ - public static BodyBuilder put(String url, - Map uriVariables) { + public static BodyBuilder put(String url, Map uriVariables) { return method(HttpMethod.PUT, url, uriVariables); } /** - * Creates a PUT builder with the given url. + * Create an HTTP PUT builder with the given url. * @param url the URL * @return the created builder */ @@ -337,31 +330,29 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a PATCH builder with the given url and uri variables. + * Create an HTTP PATCH builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template * @return the created builder */ - public static BodyBuilder patch(String url, - Object... uriVariables) { + public static BodyBuilder patch(String url, Object... uriVariables) { return method(HttpMethod.PATCH, url, uriVariables); } /** - * Creates a PATCH builder with the given url and uri variables. + * Create an HTTP PATCH builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template * @return the created builder */ - public static BodyBuilder patch(String url, - Map uriVariables) { + public static BodyBuilder patch(String url, Map uriVariables) { return method(HttpMethod.PATCH, url, uriVariables); } /** - * Creates a PATCH builder with the given url. + * Create an HTTP PATCH builder with the given url. * @param url the URL * @return the created builder */ @@ -370,31 +361,29 @@ public class RequestEntity extends HttpEntity { } /** - * Creates a DELETE builder with the given url and uri variables. + * Create an HTTP DELETE builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template * @return the created builder */ - public static HeadersBuilder delete(String url, - Object... uriVariables) { + public static HeadersBuilder delete(String url, Object... uriVariables) { return method(HttpMethod.DELETE, url, uriVariables); } /** - * Creates a DELETE builder with the given url and uri variables. + * Create an HTTP DELETE builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template * @return the created builder */ - public static HeadersBuilder delete(String url, - Map uriVariables) { + public static HeadersBuilder delete(String url, Map uriVariables) { return method(HttpMethod.DELETE, url, uriVariables); } /** - * Creates a DELETE builder with the given url. + * Create an HTTP DELETE builder with the given url. * @param url the URL * @return the created builder */ @@ -403,31 +392,29 @@ public class RequestEntity extends HttpEntity { } /** - * Creates an OPTIONS builder with the given url and uri variables. + * Create an HTTP OPTIONS builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template * @return the created builder */ - public static HeadersBuilder options(String url, - Object... uriVariables) { + public static HeadersBuilder options(String url, Object... uriVariables) { return method(HttpMethod.OPTIONS, url, uriVariables); } /** - * Creates an OPTIONS builder with the given url and uri variables. + * Creates an HTTP OPTIONS builder with the given url and uri variables. *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @param uriVariables the variables to expand in the template * @return the created builder */ - public static HeadersBuilder options(String url, - Map uriVariables) { + public static HeadersBuilder options(String url, Map uriVariables) { return method(HttpMethod.OPTIONS, url, uriVariables); } /** - * Creates an OPTIONS builder with the given url. + * Creates an HTTP OPTIONS builder with the given url. * @param url the URL * @return the created builder */ @@ -451,29 +438,29 @@ public class RequestEntity extends HttpEntity { B header(String headerName, String... headerValues); /** - * Set the list of acceptable {@linkplain MediaType media types}, as specified - * by the {@code Accept} header. + * Set the list of acceptable {@linkplain MediaType media types}, as + * specified by the {@code Accept} header. * @param acceptableMediaTypes the acceptable media types */ B accept(MediaType... acceptableMediaTypes); /** - * Set the list of acceptable {@linkplain Charset charsets}, as specified by - * the {@code Accept-Charset} header. + * Set the list of acceptable {@linkplain Charset charsets}, as specified + * by the {@code Accept-Charset} header. * @param acceptableCharsets the acceptable charsets */ B acceptCharset(Charset... acceptableCharsets); /** - * Sets the value of the {@code If-Modified-Since} header. - *

The date should be specified as the number of milliseconds since January 1, - * 1970 GMT. + * Set the value of the {@code If-Modified-Since} header. + *

The date should be specified as the number of milliseconds since + * January 1, 1970 GMT. * @param ifModifiedSince the new value of the header */ B ifModifiedSince(long ifModifiedSince); /** - * Sets the values of the {@code If-None-Match} header. + * Set the values of the {@code If-None-Match} header. * @param ifNoneMatches the new value of the header */ B ifNoneMatch(String... ifNoneMatches); @@ -493,8 +480,8 @@ public class RequestEntity extends HttpEntity { public interface BodyBuilder extends HeadersBuilder { /** - * Set the length of the body in bytes, as specified by the {@code Content-Length} - * header. + * Set the length of the body in bytes, as specified by the + * {@code Content-Length} header. * @param contentLength the content length * @return this builder * @see HttpHeaders#setContentLength(long) @@ -502,8 +489,8 @@ public class RequestEntity extends HttpEntity { BodyBuilder contentLength(long contentLength); /** - * Set the {@linkplain MediaType media type} of the body, as specified by the - * {@code Content-Type} header. + * Set the {@linkplain MediaType media type} of the body, as specified + * by the {@code Content-Type} header. * @param contentType the content type * @return this builder * @see HttpHeaders#setContentType(MediaType) @@ -511,7 +498,7 @@ public class RequestEntity extends HttpEntity { BodyBuilder contentType(MediaType contentType); /** - * Sets the body of the request entity and returns it. + * Set the body of the request entity and build the RequestEntity. * @param body the body of the request entity * @param the type of the body * @return the built request entity @@ -580,7 +567,7 @@ public class RequestEntity extends HttpEntity { @Override public RequestEntity build() { - return new RequestEntity(null, this.headers, this.method, this.url); + return new RequestEntity(this.headers, this.method, this.url); } @Override @@ -589,9 +576,4 @@ public class RequestEntity extends HttpEntity { } } - - - - - } diff --git a/spring-web/src/main/java/org/springframework/web/client/RestOperations.java b/spring-web/src/main/java/org/springframework/web/client/RestOperations.java index 6a0a291e88..d57b919f96 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestOperations.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestOperations.java @@ -463,9 +463,9 @@ public interface RestOperations { ParameterizedTypeReference responseType) throws RestClientException; /** - * Execute the HTTP method and URL of the {@link RequestEntity}, writing it to the - * request, and returns the response as {@link ResponseEntity}. Typically used in - * combination with the static builder methods on {@code RequestEntity}, for instance: + * Execute the request specified in the given {@link RequestEntity} and return + * the response as {@link ResponseEntity}. Typically used in combination + * with the static builder methods on {@code RequestEntity}, for instance: * *

 	 * MyRequest body = ...
@@ -482,9 +482,9 @@ public interface RestOperations {
 			Class responseType) throws RestClientException;
 
 	/**
-	 * Execute the HTTP method and URL of the {@link RequestEntity}, writing it to the
-	 * request, and returns the response as {@link ResponseEntity}.
-	 * The given {@link ParameterizedTypeReference} is used to pass generic type information:
+	 * Execute the request specified in the given {@link RequestEntity} and return
+	 * the response as {@link ResponseEntity}. The given
+	 * {@link ParameterizedTypeReference} is used to pass generic type information:
 	 *
 	 * 
 	 * MyRequest body = ...
diff --git a/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java b/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java
index f475b4a82d..dde39fa7d3 100644
--- a/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java
+++ b/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java
@@ -20,15 +20,20 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.Charset;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
 import static org.junit.Assert.*;
 import static org.junit.Assert.assertEquals;
 import org.junit.Test;
 
+
+/**
+ * Unit tests for {@link org.springframework.http.RequestEntity}.
+ * @author Arjen Poutsma
+ */
 public class RequestEntityTests {
 
+
 	@Test
 	public void normal() throws URISyntaxException {
 		String headerName = "My-Custom-Header";
@@ -60,7 +65,7 @@ public class RequestEntityTests {
 		RequestEntity entity = RequestEntity.method(HttpMethod.GET, url, host, path).build();
 		assertEquals(expected, entity.getUrl());
 
-		Map uriVariables = new HashMap(2);
+		Map uriVariables = new HashMap<>(2);
 		uriVariables.put("host", host);
 		uriVariables.put("path", path);
 
@@ -105,8 +110,7 @@ public class RequestEntityTests {
 
 		assertEquals("text/plain", responseHeaders.getFirst("Accept"));
 		assertEquals("utf-8", responseHeaders.getFirst("Accept-Charset"));
-		assertEquals("Thu, 01 Jan 1970 00:00:12 GMT",
-				responseHeaders.getFirst("If-Modified-Since"));
+		assertEquals("Thu, 01 Jan 1970 00:00:12 GMT", responseHeaders.getFirst("If-Modified-Since"));
 		assertEquals(ifNoneMatch, responseHeaders.getFirst("If-None-Match"));
 		assertEquals(String.valueOf(contentLength), responseHeaders.getFirst("Content-Length"));
 		assertEquals(contentType.toString(), responseHeaders.getFirst("Content-Type"));
@@ -141,7 +145,4 @@ public class RequestEntityTests {
 
 	}
 
-
-
-
 }
\ No newline at end of file
diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java
index 8374f32015..fdd32ae81d 100644
--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java
+++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2013 the original author or authors.
+ * Copyright 2002-2014 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.
@@ -38,10 +38,10 @@ import org.springframework.web.context.request.NativeWebRequest;
 import org.springframework.web.method.support.ModelAndViewContainer;
 
 /**
- * Resolves {@link HttpEntity} method argument values and also handles
- * both {@link HttpEntity} and {@link ResponseEntity} return values.
+ * Resolves {@link HttpEntity} and {@link RequestEntity} method argument values
+ * and also handles {@link HttpEntity} and {@link ResponseEntity} return values.
  *
- * 

An {@link HttpEntity} return type has a set purpose. Therefore this + *

An {@link HttpEntity} return type has a specific purpose. Therefore this * handler should be configured ahead of handlers that support any return * value type annotated with {@code @ModelAttribute} or {@code @ResponseBody} * to ensure they don't take over. diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java index 7397465d4c..9af77258db 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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.