The Response class provides shortcut methods for handling
the more common responses that a RESTful service will need. These methods handle setting
the proper headers using either provided values or default values. They also handle populating
the entity body when appropriate.
When a request is successfully processed the application needs to send a response to acknowledge that the request has been fulfilled. That response may contain an entity.
The most common response when successfully completing a response is OK. An
OK response typically contains an entity that corresponds to the request. The
Response class has an overloaded ok() method that
sets the response status to 200 and adds a supplied entity to the enclosed response
builder. There are five versions of the ok() method. The most commonly
used variant are:
Response.ok()—creates a response with a status of200and an empty entity body.Response.ok(java.lang.Object entity)—creates a response with a status of200, stores the supplied object in the responses entity body, and determines the entities media type by introspecting the object.
Example 4.3 shows an example of creating a response with an
OK status.
Example 4.3. Creating a response with an 200 response
import javax.ws.rs.core.Response;
import demo.jaxrs.server.Customer;
...
Customer customer = new Customer("Jane", 12);
return Response.ok(customer).build();For cases where the requester is not expecting an entity body, it may be more appropriate to
send a 204 No Content status instead of an 200 OK status. The
Response.noContent() method will create an appropriate response object.
Example 4.4 shows an example of creating a response with an
204 status.
Example 4.4. Creating a response with a 204 status
import javax.ws.rs.core.Response; return Response.noContent().build();
The Response class provides methods for handling three of the
redirection response statuses.
303 See OtherThe
303 See Otherstatus is useful when the requested resource needs to permanently redirect the consumer to a new resource to process the request.The
ResponseclassesseeOther()method creates a response with a303status and places the new resource URI in the message'sLocationfield. TheseeOther()method takes a single parameter that specifies the new URI as ajava.net.URIobject.304 Not ModifiedThe
304 Not Modifiedstatus can be used for different things depending on the nature of the request. It can be used to signify that the requested resource has not changed since a previousGETrequest. It can also be used to signify that a request to modify the resource did not result in the resource being changed.The
ResponseclassesnotModified()methods creates a response with a304status and sets the modified date property on the HTTP message. There are three versions of thenotModified()method:notModified();notModified(javax.ws.rs.core.Entity tag);notModified(java.lang.String tag);
307 Temporary RedirectThe
307 Temporary Redirectstatus is useful when the requested resource needs to direct the consumer to a new resource, but wants the consumer to continue using this resource to handle future requests.The
ResponseclassestemporaryRedirect()method creates a response with a307status and places the new resource URI in the message'sLocationfield. ThetemporaryRedirect()method takes a single parameter that specifies the new URI as ajava.net.URIobject.
Example 4.5 shows an example of creating a response with an
304 status.
Example 4.5. Creating a response with a 304 status
import javax.ws.rs.core.Response; return Response.notModified().build();
The Response class provides methods to create responses for two basic
processing errors:
serverError()();—creates a response with a status of500 Internal Server Error.notAcceptable()(java.util.List<javax.ws.rs.core.Variant> variants);—creates a response with a406 Not Acceptablestatus and an entity body containing a list of acceptable resource types.
Example 4.6 shows an example of creating a response with an
500 status.
Example 4.6. Creating a response with a 500 status
import javax.ws.rs.core.Response; return Response.serverError().build();








