Created
May 15, 2014 10:15
-
-
Save philwebb/6c168d00d06dbb97359d to your computer and use it in GitHub Desktop.
No Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2002-2012 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. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package sample3; | |
import java.net.URI; | |
import java.util.Arrays; | |
import java.util.LinkedHashSet; | |
import java.util.Set; | |
import org.springframework.http.HttpEntity; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpMethod; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.MediaType; | |
import org.springframework.util.MultiValueMap; | |
import org.springframework.util.ObjectUtils; | |
/** | |
* Extension of {@link HttpEntity} that adds a {@link HttpStatus} status code. | |
* | |
* <p>Returned by {@link org.springframework.web.client.RestTemplate#getForEntity}: | |
* <pre class="code"> | |
* ResponseEntity<String> entity = template.getForEntity("http://example.com", String.class); | |
* String body = entity.getBody(); | |
* MediaType contentType = entity.getHeaders().getContentType(); | |
* HttpStatus statusCode = entity.getStatusCode(); | |
* </pre> | |
* <p>Can also be used in Spring MVC, as a return value from a @Controller method: | |
* <pre class="code"> | |
* @RequestMapping("/handle") | |
* public ResponseEntity<String> handle() { | |
* HttpHeaders responseHeaders = new HttpHeaders(); | |
* responseHeaders.set("MyResponseHeader", "MyValue"); | |
* return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED); | |
* } | |
* </pre> | |
* | |
* @author Arjen Poutsma | |
* @since 3.0.2 | |
* @see #getStatusCode() | |
*/ | |
public class ResponseEntity<T> extends HttpEntity<T> { | |
private final HttpStatus statusCode; | |
/** | |
* Create a new {@code ResponseEntity} with the given status code, and no body nor headers. | |
* @param statusCode the status code | |
*/ | |
public ResponseEntity(HttpStatus statusCode) { | |
super(); | |
this.statusCode = statusCode; | |
} | |
/** | |
* Create a new {@code ResponseEntity} with the given body and status code, and no headers. | |
* @param body the entity body | |
* @param statusCode the status code | |
*/ | |
public ResponseEntity(T body, HttpStatus statusCode) { | |
super(body); | |
this.statusCode = statusCode; | |
} | |
/** | |
* Create a new {@code HttpEntity} with the given headers and status code, and no body. | |
* @param headers the entity headers | |
* @param statusCode the status code | |
*/ | |
public ResponseEntity(MultiValueMap<String, String> headers, HttpStatus statusCode) { | |
super(headers); | |
this.statusCode = statusCode; | |
} | |
/** | |
* Create a new {@code HttpEntity} with the given body, headers, and status code. | |
* @param body the entity body | |
* @param headers the entity headers | |
* @param statusCode the status code | |
*/ | |
public ResponseEntity(T body, MultiValueMap<String, String> headers, HttpStatus statusCode) { | |
super(body, headers); | |
this.statusCode = statusCode; | |
} | |
/** | |
* Return the HTTP status code of the response. | |
* @return the HTTP status as an HttpStatus enum value | |
*/ | |
public HttpStatus getStatusCode() { | |
return this.statusCode; | |
} | |
@Override | |
public boolean equals(Object other) { | |
if (this == other) { | |
return true; | |
} | |
if (!(other instanceof ResponseEntity)) { | |
return false; | |
} | |
ResponseEntity<?> otherEntity = (ResponseEntity<?>) other; | |
return (ObjectUtils.nullSafeEquals(this.statusCode, otherEntity.statusCode) && super.equals(other)); | |
} | |
@Override | |
public int hashCode() { | |
return super.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.statusCode); | |
} | |
@Override | |
public String toString() { | |
StringBuilder builder = new StringBuilder("<"); | |
builder.append(this.statusCode.toString()); | |
builder.append(' '); | |
builder.append(this.statusCode.getReasonPhrase()); | |
builder.append(','); | |
T body = getBody(); | |
HttpHeaders headers = getHeaders(); | |
if (body != null) { | |
builder.append(body); | |
if (headers != null) { | |
builder.append(','); | |
} | |
} | |
if (headers != null) { | |
builder.append(headers); | |
} | |
builder.append('>'); | |
return builder.toString(); | |
} | |
/** | |
* Add the given, single header value under the given name. | |
* @param headerName the header name | |
* @param headerValues the header value(s) | |
* @return a new builder | |
* @see HttpHeaders#add(String, String) | |
*/ | |
public ResponseEntity<T> header(String headerName, String... headerValues) { | |
HttpHeaders newHeaders = newHeaders(); | |
for (String headerValue : headerValues) { | |
newHeaders.add(headerName, headerValue); | |
} | |
return withNewHeaders(newHeaders); | |
} | |
/** | |
* Set the set of allowed {@link HttpMethod HTTP methods}, as specified by the | |
* {@code Allow} header. | |
* @param allowedMethods the allowed methods | |
* @return a new builder | |
* @see HttpHeaders#setAllow(Set) | |
*/ | |
public ResponseEntity<T> allow(HttpMethod... allowedMethods) { | |
HttpHeaders newHeaders = newHeaders(); | |
newHeaders.setAllow(new LinkedHashSet<HttpMethod>(Arrays.asList(allowedMethods))); | |
return withNewHeaders(newHeaders); | |
} | |
/** | |
* Sets the entity tag of the body, as specified by the {@code ETag} header. | |
* @param eTag the new entity tag | |
* @return a new builder | |
* @see HttpHeaders#setETag(String) | |
*/ | |
public ResponseEntity<T> eTag(String eTag) { | |
HttpHeaders newHeaders = newHeaders(); | |
newHeaders.setETag(eTag); | |
return withNewHeaders(newHeaders); | |
} | |
/** | |
* Sets the time the resource was last changed, as specified by the {@code | |
* Last-Modified} header. <p>The date should be specified as the number of | |
* milliseconds since January 1, 1970 GMT. | |
* @param lastModified the last modified date | |
* @return a new builder | |
* @see HttpHeaders#setLastModified(long) | |
*/ | |
public ResponseEntity<T> lastModified(long lastModified) { | |
HttpHeaders newHeaders = newHeaders(); | |
newHeaders.setLastModified(lastModified); | |
return withNewHeaders(newHeaders); | |
} | |
/** | |
* Set the location of a resource, as specified by the {@code Location} header. | |
* @param location the location | |
* @return a new builder | |
* @see HttpHeaders#setLocation(URI) | |
*/ | |
public ResponseEntity<T> location(URI location) { | |
HttpHeaders newHeaders = newHeaders(); | |
newHeaders.setLocation(location); | |
return withNewHeaders(newHeaders); | |
} | |
/** | |
* Set the length of the body in bytes, as specified by the {@code Content-Length} | |
* header. | |
* @param contentLength the content length | |
* @return a new builder | |
* @see HttpHeaders#setContentLength(long) | |
*/ | |
public ResponseEntity<T> contentLength(long contentLength) { | |
HttpHeaders newHeaders = newHeaders(); | |
newHeaders.setContentLength(contentLength); | |
return withNewHeaders(newHeaders); | |
} | |
/** | |
* Set the {@linkplain MediaType media type} of the body, as specified by the | |
* {@code Content-Type} header. | |
* @param mediaType the content media type | |
* @return a new builder | |
* @see HttpHeaders#setContentType(MediaType) | |
*/ | |
public ResponseEntity<T> contentType(MediaType mediaType) { | |
HttpHeaders newHeaders = newHeaders(); | |
newHeaders.setContentType(mediaType); | |
return withNewHeaders(newHeaders); | |
} | |
protected final HttpHeaders newHeaders() { | |
HttpHeaders headers = new HttpHeaders(); | |
headers.putAll(getHeaders()); | |
return headers; | |
} | |
protected final ResponseEntity<T> withNewHeaders(HttpHeaders newHeaders) { | |
return new ResponseEntity<T>(getBody(), newHeaders, getStatusCode()); | |
} | |
public static <T> ResponseEntity<T> ok() { | |
return new ResponseEntity<T>(null, null, HttpStatus.OK); | |
} | |
public static <T> ResponseEntity<T> ok(T body) { | |
return new ResponseEntity<T>(body, null, HttpStatus.OK); | |
} | |
public static <T> ResponseEntity<T> created(URI location) { | |
return new ResponseEntity<T>(null, null, HttpStatus.CREATED).location(location); | |
} | |
public static <T> ResponseEntity<T> created(URI location, T body) { | |
return new ResponseEntity<T>(body, null, HttpStatus.CREATED).location(location); | |
} | |
public static <T> ResponseEntity<T> accepted() { | |
return new ResponseEntity<T>(null, null, HttpStatus.ACCEPTED); | |
} | |
public static <T> ResponseEntity<T> accepted(T body) { | |
return new ResponseEntity<T>(body, null, HttpStatus.ACCEPTED); | |
} | |
public static <T> ResponseEntity<T> noContent() { | |
return new ResponseEntity<T>(null, null, HttpStatus.NO_CONTENT); | |
} | |
public static <T> ResponseEntity<T> status(HttpStatus status) { | |
return new ResponseEntity<T>(null, null, status); | |
} | |
public static <T> ResponseEntity<T> status(HttpStatus status, T body) { | |
return new ResponseEntity<T>(body, null, status); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample3; | |
import org.springframework.http.MediaType; | |
public class ResponseEntityUsage { | |
public ResponseEntity<Void> a() { | |
return ResponseEntity.ok(); | |
} | |
public ResponseEntity<?> a2() { | |
return ResponseEntity.ok(); | |
} | |
public ResponseEntity<String> b() { | |
return ResponseEntity.ok("test"); | |
} | |
public ResponseEntity<String> c() { | |
String body = ""; | |
return ResponseEntity.ok(body).contentType(MediaType.APPLICATION_JSON); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment