Enterprise Java Development@TOPIC@

Chapter 110. Resource Examples

Method Implementation Examples

110.1. JAX-RS Resource Class
110.2. JAX-RS GET Resource Collection
110.2.1. Server-side GET Resource Collection
110.2.2. Client-side GET Resource Collection
110.3. JAX-RS Resource POST Method
110.3.1. Server-side POST Resource Collection
110.3.2. Client-side POST Resource Collection
110.4. JAX-RS GET Resource Single Method
110.4.1. Server-side GET Single Resource
110.4.2. Client-side GET Single Resource
110.5. JAX-RS PUT (Nested) Resource Method
110.5.1. Server-Side JAX-RS PUT Resource Method
110.5.2. Client-side JAX-RS PUT Resource Method
110.6. JAX-RS DELETE Resource Method
110.6.1. Server-side JAX-RS DELETE Resource Method
110.6.2. Client-side JAX-RS DELETE Resource Method
110.7. Summary
# client

private UriBuilder getBaseUrl(String...path) {
    UriBuilder builder = UriBuilder.fromUri(baseUrl);        
    if (path!=null) {
        for (String p:path) {
            builder = builder.path(p);
        }
    }
    return builder;
}
# client

    static String TODO_LISTS_PATH = "todo_lists";
# client

public Response getTodoLists(Integer offset, Integer limit) {
    URI uri = getBaseUrl(TODO_LISTS_PATH).build();
    WebTarget target = client.target(uri);
    if (offset!=null) {
        target=target.queryParam(OFFSET, offset);
    }
    if (limit!=null) {
        target=target.queryParam(LIMIT, limit);
    }
    return target.request(mediaType)
          .buildGet()
          .invoke();
}
# client

static <T> T getEntity(Response response, Class<T> type) {
    if (Response.Status.Family.SUCCESSFUL.equals(response.getStatusInfo().getFamily())) {
        return response.readEntity(type, type.getAnnotations());
    } else {
# client

Response response = todosClient.getTodoLists(null, null);
TodoListListDTO todoLists = getEntity(response, TodoListListDTO.class);
private ResponseBuilder getBadRequestResponse(Exception ex) {

    logger.debug(ex.getMessage());
    return Response.status(Status.BAD_REQUEST)
            .entity(new MessageDTO(ex.getMessage()));        
}
@POST

@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response createTodoList(TodoListDTO todoList) {
    ResponseBuilder rb = null;
    try {
        TodoListDTO entity = todosMgmt.createTodoList(todoList);
        URI location = uriInfo.getBaseUriBuilder()
                .path(TodoListsResource.class)
                .path(TodoListsResource.class, "getTodoList")
                .build(entity.getName());
        rb = Response.created(location)                    
                .contentLocation(location)
                .entity(entity);
    } catch (InvalidRequestException ex) {
        rb = getBadRequestResponse(ex);
    } catch (InternalErrorException ex) {
        rb = getInternalErrorResponse(ex);
    } catch (Exception ex) {
        rb = getUndexpectedErrorResponse("Unexpected error creating TodoList", ex);
    }
    return rb.build();
}