Custom responses
As discussed earlier, any response from a RESTful service may include:
- Status code
- Response entity
- Content type
By default, any RESTful service that runs and returns normally without any problems will contain the status code 200
(OK). The response entity will be these value you return from the RESTful method. The content type will be these one specified by the @Produces
annotation, as mentioned earlier.
Sometimes, you may need to customize the details of the response yourself at runtime, in one of the following scenarios:
- Â The response entity data type/MIME content type is not known until runtime
- Â A custom status code should be returned to the client according to some business logic inside the RESTful service code
In JAX-RS, you can customize the response at runtime by following two steps:
- Â Declaring the return type of the method to be of type
Response
- Â Building a custom response object from inside your RESTful service code and returning it to the client
Building a response
object...