Returning modified resources to the caller

In a typical REST request-response model, an API client reads a resource from the server, makes some modifications, and sends the modified resource back to the server to save the changes via the PUT, POST, or PATCH operations as appropriate. While persisting changes, there are chances that the server may modify some of the fields, such as the version field and the modification date. In such cases, it makes sense to return the modified resource representation back to the client in order to keep both the client and the server in sync. The following example returns the modified Department entity back to the caller:

@POST 
@Path("{id}") 
public Department editDepartment(@PathParam("id") Short id, 
Department entity) { Department modifiedEntity=editDepartmentEntity(entity); return modifiedEntity; }

If you are using the POST operation for creating a resource, you can use the HTTP 201 status code in the response, indicating the status of operation, and include a Location header that points to the URL of the new resource. Here is an example:

import javax.ws.rs.core.Response; 
import javax.ws.rs.core.UriInfo; 
import javax.ws.rs.core.Context; 
 
@POST 
public Response createDepartment(Department entity, 
    @Context UriInfo uriInfo) { 
    Integer deptId=createDepartmentEntity(entity); 
    //Builds a URI for the newly created resource 
    UriBuilder builder = uriInfo.getAbsolutePathBuilder(); 
    builder.path(deptId.toString()); 
    return Response.created(builder.build()).build(); 
} 
..................Content has been hidden....................

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