- Response data type in java
- Nested Class Summary
- Constructor Summary
- Method Summary
- Methods inherited from class java.lang.Object
- Constructor Detail
- Response
- Method Detail
- getStatus
- getStatusInfo
- getEntity
- readEntity
- readEntity
- readEntity
- readEntity
- hasEntity
- bufferEntity
- close
- getMediaType
- getLanguage
- Response data type in java
- Nested Class Summary
- Method Summary
- Method Detail
- statusCode
- request
- previousResponse
- headers
- body
- sslSession
- uri
- version
Response data type in java
Defines the contract between a returned instance and the runtime when an application needs to provide meta-data to the runtime. An application class should not extend this class directly. Response class is reserved for an extension by a JAX-RS implementation providers. An application should use one of the static methods to create a Response instance using a ResponseBuilder. Several methods have parameters of type URI, UriBuilder provides convenient methods to create such values as does URI.create(java.lang.String) .
Nested Class Summary
A class used to build Response instances that contain metadata instead of or in addition to an entity.
Constructor Summary
Protected constructor, use one of the static methods to obtain a Response.ResponseBuilder instance and obtain a Response from that.
Method Summary
Close the underlying message entity input stream (if available and open) as well as releases any other resources associated with the response (e.g.
Create a new ResponseBuilder for a created resource, set the location header using the supplied value.
Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type.
Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type.
Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type.
Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type.
Methods inherited from class java.lang.Object
Constructor Detail
Response
Protected constructor, use one of the static methods to obtain a Response.ResponseBuilder instance and obtain a Response from that.
Method Detail
getStatus
public abstract int getStatus()
getStatusInfo
getEntity
Get the message entity Java instance. Returns null if the message does not contain an entity body. If the entity is represented by an un-consumed input stream the method will return the input stream.
readEntity
Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type. Method throws an ProcessingException if the content of the message cannot be mapped to an entity of the requested type and IllegalStateException in case the entity is not backed by an input stream or if the original entity input stream has already been consumed without buffering the entity data prior consuming. A message instance returned from this method will be cached for subsequent retrievals via getEntity() . Unless the supplied entity type is an input stream , this method automatically closes the an unconsumed original response entity data stream if open. In case the entity data has been buffered, the buffer will be reset prior consuming the buffered data to enable subsequent invocations of readEntity(. ) methods on this response.
readEntity
Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type. Method throws an ProcessingException if the content of the message cannot be mapped to an entity of the requested type and IllegalStateException in case the entity is not backed by an input stream or if the original entity input stream has already been consumed without buffering the entity data prior consuming. A message instance returned from this method will be cached for subsequent retrievals via getEntity() . Unless the supplied entity type is an input stream , this method automatically closes the an unconsumed original response entity data stream if open. In case the entity data has been buffered, the buffer will be reset prior consuming the buffered data to enable subsequent invocations of readEntity(. ) methods on this response.
readEntity
public abstract T readEntity(Class entityType, Annotation[] annotations)
Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type. Method throws an ProcessingException if the content of the message cannot be mapped to an entity of the requested type and IllegalStateException in case the entity is not backed by an input stream or if the original entity input stream has already been consumed without buffering the entity data prior consuming. A message instance returned from this method will be cached for subsequent retrievals via getEntity() . Unless the supplied entity type is an input stream , this method automatically closes the an unconsumed original response entity data stream if open. In case the entity data has been buffered, the buffer will be reset prior consuming the buffered data to enable subsequent invocations of readEntity(. ) methods on this response.
readEntity
public abstract T readEntity(GenericType entityType, Annotation[] annotations)
Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type. Method throws an ProcessingException if the content of the message cannot be mapped to an entity of the requested type and IllegalStateException in case the entity is not backed by an input stream or if the original entity input stream has already been consumed without buffering the entity data prior consuming. A message instance returned from this method will be cached for subsequent retrievals via getEntity() . Unless the supplied entity type is an input stream , this method automatically closes the an unconsumed original response entity data stream if open. In case the entity data has been buffered, the buffer will be reset prior consuming the buffered data to enable subsequent invocations of readEntity(. ) methods on this response.
hasEntity
public abstract boolean hasEntity()
Check if there is an entity available in the response. The method returns true if the entity is present, returns false otherwise. Note that the method may return true also for response messages with a zero-length content, in case the "Content-Length" and "Content-Type" headers are specified in the message. In such case, an attempt to read the entity using one of the readEntity(. ) methods will return a corresponding instance representing a zero-length entity for a given Java type or produce a ProcessingException in case no such instance is available for the Java type.
bufferEntity
public abstract boolean bufferEntity()
Buffer the message entity data. In case the message entity is backed by an unconsumed entity input stream, all the bytes of the original entity input stream are read and stored in a local buffer. The original entity input stream is consumed and automatically closed as part of the operation and the method returns true . In case the response entity instance is not backed by an unconsumed input stream an invocation of bufferEntity method is ignored and the method returns false . This operation is idempotent, i.e. it can be invoked multiple times with the same effect which also means that calling the bufferEntity() method on an already buffered (and thus closed) message instance is legal and has no further effect. Also, the result returned by the bufferEntity() method is consistent across all invocations of the method on the same Response instance. Buffering the message entity data allows for multiple invocations of readEntity(. ) methods on the response instance. Note however, that once the response instance itself is closed , the implementations are expected to release the buffered message entity data too. Therefore any subsequent attempts to read a message entity stream on such closed response will result in an IllegalStateException being thrown.
close
public abstract void close()
Close the underlying message entity input stream (if available and open) as well as releases any other resources associated with the response (e.g. buffered message entity data ). This operation is idempotent, i.e. it can be invoked multiple times with the same effect which also means that calling the close() method on an already closed message instance is legal and has no further effect. The close() method should be invoked on all instances that contain an un-consumed entity input stream to ensure the resources associated with the instance are properly cleaned-up and prevent potential memory leaks. This is typical for client-side scenarios where application layer code processes only the response headers and ignores the response entity. Any attempts to manipulate (read, get, buffer) a message entity on a closed response will result in an IllegalStateException being thrown.
getMediaType
getLanguage
Response data type in java
An HTTP response. An HttpResponse is not created directly, but rather returned as a result of sending an HttpRequest . An HttpResponse is made available when the response status code and headers have been received, and typically after the response body has also been completely received. Whether or not the HttpResponse is made available before the response body has been completely received depends on the BodyHandler provided when sending the HttpRequest . This class provides methods for accessing the response status code, headers, the response body, and the HttpRequest corresponding to this response. The following is an example of retrieving a response as a String:
HttpResponse response = client .send(request, BodyHandlers.ofString());
The class BodyHandlers provides implementations of many common response handlers. Alternatively, a custom BodyHandler implementation can be used.
Nested Class Summary
Implementations of BodyHandler that implement various useful handlers, such as handling the response body as a String, or streaming the response body to a file.
Implementations of BodySubscriber that implement various useful subscribers, such as converting the response body bytes into a String, or streaming the bytes to a file.
Initial response information supplied to a BodyHandler when a response is initially received and before the body is processed.
Method Summary
Method Detail
statusCode
request
Returns the HttpRequest corresponding to this response. The returned HttpRequest may not be the initiating request provided when sending. For example, if the initiating request was redirected, then the request returned by this method will have the redirected URI, which will be different from the initiating request URI.
previousResponse
OptionalHttpResponseT>> previousResponse()
Returns an Optional containing the previous intermediate response if one was received. An intermediate response is one that is received as a result of redirection or authentication. If no previous response was received then an empty Optional is returned.
headers
body
Returns the body. Depending on the type of T , the returned body may represent the body after it was read (such as byte[] , or String , or Path ) or it may represent an object with which the body is read, such as an InputStream . If this HttpResponse was returned from an invocation of previousResponse() then this method returns null
sslSession
Returns an Optional containing the SSLSession in effect for this response. Returns an empty Optional if this is not a HTTPS response.
uri
Returns the URI that the response was received from. This may be different from the request URI if redirection occurred.
version
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.