Programmatically building header links using JAX-RS APIs

The JAX-RS APIs allow you to add links to the HTTP header content as well. In the following code snippet, a link to the access manager for a department resource is added to the HTTP response header:

//Rest of the code is omitted for brevity  
@GET 
@Path("{id}") 
@Produces(MediaType.APPLICATION_JSON) 
public Response find(@PathParam("id") Short id) { 
 
  Department deptEntity = findDepartmentEntity(id); 
   //Add links to header 
  return  
     Response.ok() 
        .links(getLinks(deptEntity.getManagerId())) 
        .entity(deptEntity).build(); 
 
} 
//Get the link object representing URI for manager 
private Link[] getLinks(int departmentId) { 
  Link managerLink = Link.fromUri("{id}/employees") 
      .rel("manager").build(departmentId); 
  return new Link[]{managerLink}; 
} 

The following is the sample response generated by the preceding resource method implementation for the GET /departments/60 HTTP/1.1 request:

Header
Server: GlassFish Server Link: <103/employees>; rel="manager" Content-Type: application/json Date: Sun, 29 Mar 2015 11:12:26 GMT Content-Length: 124 Body
{ departmentId: 60 departmentName: "IT" locationId: 1400 managerId: 103 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.251.70