uCosminexus Application Server, Web Service Development Guide

[Contents][Glossary][Index][Back][Next]

17.1.8 Exception handling

The JAX-RS engine handles the exceptions thrown from the following locations as explained in this sub-section:

Organization of this subsection
(1) WebApplicationException (When no exception mapping provider exists)
(2) Other exceptions (When no exception mapping provider exists)
(3) If the exception mapping provider exists

(1) WebApplicationException (When no exception mapping provider exists)

If the system throws WebApplicationException and no exception mapping provider corresponding to WebApplicationException or the respective parent exists, the JAX-RS engine handles WebApplicationException as described in the following table.

Table 17-10 Handling WebApplicationException (When no exception mapping provider exists)

No. Condition Handling result
Response property HTTP status code of the response property HTTP status code of the HTTP response Message ID
1 Set
  • 499 or less
  • The values of the enumerated type Response.Status
The value held by the response property of WebApplicationException is used. KDJJ30021-I
2 Set
  • 499 or less
  • Values that do not exist in the enumerated type Response.Status
The value held by the response property of WebApplicationException is used. KDJJ30022-I
3 Set
  • 500 or more
  • The values of the enumerated type Response.Status
The value held by the response property of WebApplicationException is used. KDJJ10018-E
4 Set
  • 500 or more
  • Values that do not existing in the enumerated type Response.Status
The value held by the response property of WebApplicationException is used. KDJJ10019-E
5 Not set -- 500 KDJJ10018-E

Legends:
--: Not applicable

An example of generating WebApplicationException and setting the response property is as follows.

package com.sample.resources;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
 
//root resource class 
@Path("/root")
public class Resource { 
 
  //subresource method
  @Path("/subresourcemethod")
  @GET
  public String subResourceMethod() {
    //Use the ResponseBuilder to generate the Response instance 
    ResponseBuilder rb = Response.status(208).
    entity("entity for WebApplicationException");
 
    //set the Response instance to the WebApplicationException
    throw new WebApplicationException(rb.build());
}
}

You consider the context root of the Web application (WAR file) containing the root resource class com.sample.resources.Resource to be "resource" and that the Web application is published on a host called "example.com". In this case, the HTTP GET request corresponding to the URL "http://example.com/resource/root/subresourcemethod" is dispatched to the method subResourceMethod(). The HTTP response is converted from the response property of WebApplicationException.

(2) Other exceptions (When no exception mapping provider exists)

If system throws an exception other than WebApplicationException and if the exception mapping provider corresponding to the WebApplicationException exception or the respective parent does not exist, the JAX-RS engine handles WebApplicationException as described in the following table:

Table 17-11 Other exceptions (When no exception mapping provider exists)

No. Conditions Handling result
Exception type Operation by the JAX-RS engine HTTP status code of the HTTP response Message ID
1 Runtime exception Throws a runtime exception again. 500 KDJJ10010-E,
KDJJ10039-E
2 Other than the aforementioned Wraps the exception in RuntimeException and then throws the exception. 500 KDJJ10017-E,
KDJJ10039-E

(3) If the exception mapping provider exists

If the exception mapping provider corresponding to the thrown exception or respective parent exists, the exception handling depends on the operations of the exception mapping provider. Note that if multiple exception mapping providers corresponding to the thrown exception or respective parent exist, the exception mapping provider that can handle the exception closest to the exceptions (including the thrown exceptions) handles the exception.