Java Magazine, Jan/Feb 2016
WebApplicationException ServerErrorException ClientErrorException RedirectionException ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 39 web apps exceptions that inherit from javax ws rs Web ApplicationException This design relieves you from having to build and throw a WebApplicationException with explicit HTTP error information The new exceptions are intuitively named and each of them maps to a specific HTTP error scenario by default This means that throwing any of these exceptions from your resource classes will result in a predefined as per mapping HTTP error response being sent to the client that is the client would receive an HTTP 403 in the event that you throw a NotAuthorizedException detailed mapping in Table 1 The exceptions behavior can also be modified at runtime by using the ExceptionMapper to return a diferent Response object Figure 2 shows the new exception hierarchy A mapping of these exception classes to the corresponding HTTP error codes is shown in Table 1 Other Notable Enhancements Apart from the big ticket features several other useful enhancements were introduced as part of JAX RS 20 which you should use if they fit your application @ BeanParam This annotation can be used to inject a custom POJO or bean whose instances can be annotated with various Param annotations such as @ HeaderParam @ CookieParam @ PathParam @ QueryParam and so on As shown below it provides a convenient way to capture HTTP URI parameters with the help of simple POJOs instead of injecting individual components from the HTTP request into JAX RS resources public class CustomerSearchRequest @ QueryParam id private String userid @ HeaderParam Accept private String accept @ CookieParam lastAccessed private Date lastAccessed getters to fetch the values The JAX RS runtime injects an instance of the @ javax ws rs BeanParam annotated method argument or an instance variable @ ConstrainedTo There are certain provider components in JAX RS that are applicable to the server side and the client side Interceptors are a good example where both ServiceUnavailableException InternalServerErrorException BadRequestException ForbiddenException NotAcceptableException NotAuthorizedException NotFoundException NotSupportedException ReaderInterceptor and WriterInterceptor can be applied on both the server side and the client side The NotAllowedException @ javax ws rs ConstrainedTo annotation can applied on Figure 2 The hierarchy of JAX RS 20 exceptions a provider class in order to explicitly restrict its contextual
You must have JavaScript enabled to view digital editions.