Enterprise Java Development@TOPIC@

Chapter 108. JAX-RS Resource/EJB Integration

108.1. EJB Injection
108.2. Candidate EJB/Business Tier Exceptions for Web API Status
108.2.1. Client Error
108.2.2. Service Error
108.3. Resource/EJB method
108.4. Summary

Request failed because of a client request error

public class ClientErrorException extends Exception {

    public ClientErrorException(String msg) {
        super(msg);
    }
}

  1. Define basic EJB method

    GreetingEJB.java
    
    import javax.ejb.TransactionAttribute;
    import javax.ejb.TransactionAttributeType;
    GreetingEJB.java
    
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public String greet(String name) {
        return String.format("hello %s", name); //core business code
    }
  2. Define Resource method in terms of calling EJB method

    GreetingsResource.java
    
    @GET
    @Path("greet")
    @Produces(MediaType.TEXT_PLAIN)
    public Response greet(@QueryParam("name") String name) {
        ResponseBuilder rb=null;
        String entity = greetingEJB.greet(name);
        rb = Response.ok(entity);
        return rb.build();
    }
  3. Add error logic to EJB/business method

    GreetingEJB.java
    
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public String greet(String name) throws InvalidRequestException {
        try {
            if (name==null || name.isEmpty()) {
                throw new InvalidRequestException("Unable to greet, name not supplied");
            }
            
            return String.format("hello %s", name); //core business code
        } catch (RuntimeException ex) {
            throw new InternalErrorException("Internal error greeting name[%s]", name);
        }
    }
  4. Add minimal exception handling in resource method

    GreetingsResource.java
    
    @GET
    @Path("greet")
    @Produces(MediaType.TEXT_PLAIN)
    public Response greet(@QueryParam("name") String name) {
        ResponseBuilder rb=null;
        try {
            String entity = greetingEJB.greet(name);
            rb = Response.ok(entity);
        } catch (Exception ex) {
            rb=Response.serverError()
                .entity(String.format("unexpected error greeting name[%s]", name));
        }
        return rb.build();
    }
  5. Add complete error reporting logic to resource method

    GreetingsResource.java
    
    @GET
    @Path("greet")
    @Produces(MediaType.TEXT_PLAIN)
    public Response greet(@QueryParam("name") String name) {
        ResponseBuilder rb=null;
        try {
            String entity = greetingEJB.greet(name);
            rb = Response.ok(entity);
        } catch (InvalidRequestException ex) {
            rb = Response.status(Status.BAD_REQUEST)
                    .entity(ex.getMessage());
        } catch (InternalErrorException ex) {
            rb = Response.status(Status.INTERNAL_SERVER_ERROR)
                    .entity(ex.getMessage());
        } catch (Exception ex) {
            rb=Response.serverError()
                .entity(String.format("unexpected error greeting name[%s]", name));
        }
        return rb.build();
    }