3.1.3.3. Create Update Function

This section describes the update function based on an example application.

Description of the function to be created
This function updates the project information that matches the project ID in the database by setting the JSON format project information in the request body during PUT requests.
Communication confirmation procedure
  1. Check the DB status in advance

    Execute the following SQL from the console of H2 and check the update target record.

    SELECT * FROM PROJECT WHERE PROJECT_ID = 1;
    
  2. Update the project information

Use any REST client to send the following request.

URL
http://localhost:9080/projects
HTTP method
PUT
Content-Type
application/json
Request body
{
    "projectId": 1,
    "projectName": "Project 999",
    "projectType": "development",
    "projectClass": "ss",
    "projectManager": "Yamada",
    "projectLeader": "Tanaka",
    "clientId": 10,
    "projectStartDate": "20160101",
    "projectEndDate": "20161231",
    "note": "Remarks 999",
    "sales": 10000,
    "costOfGoodsSold": 20000,
    "sga": 30000,
    "allocationOfCorpExpenses": 40000,
    "version": 1
}
  1. Communication confirmation

Execute the following SQL from the console of H2 and confirm that the record has been updated.

SELECT * FROM PROJECT WHERE PROJECT_ID = 1;

3.1.3.3.1. Update the project information

Create a form

Create a form to accept the value submitted by the client.

ProjectUpdateForm.java
public class ProjectUpdateForm implements Serializable {

    // Partial excerpt

    /** Project name */
    @Required
    @Domain("id")
    private String projectId;

    /** Project name */
    @Required
    @Domain("projectName")
    private String projectName;

    /** Project type */
    @Required
    @Domain("projectType")
    private String projectType;

    // Getter and setter are omitted
}
Key points of this implementation
  • All properties are declared as String type. For more information, see how to set validation rules .
Implementation of a business action method

Implement the process to update the project information in the database.

ProjectAction.java
@Consumes(MediaType.APPLICATION_JSON)
@Valid
public HttpResponse update(ProjectUpdateForm form) {
    Project project = BeanUtil.createAndCopy(Project.class, form);

    UniversalDao.update(project);

    return new HttpResponse(HttpResponse.Status.OK.getStatusCode());
}
Point of this implementation
  • To accept the request body in JSON format, specify Consumes in the MediaType.APPLICATION_JSON annotation.
  • Validates the request using the Valid annotation. For details, see JAX-RS BeanValidation Handler.
  • Create an entity from a form with BeanUtil and update the project information using Universal DAO.
  • If the update is successful, HttpResponse , which indicates a successful completion (status code:200) is returned.

Tip

In the example application, ErrorResponseBuilder is uniquely extended, response 404 if NoDataException occurs and 409 if OptimisticLockException occurs is generated and returned to the client.

Define the mapping to the URL

Use Routing Adapter to map business actions and URLs. Use Path annotation for JAX-RS for mapping.

ProjectAction.java
@Path("/projects")
public class ProjectAction {
  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  @Valid
  public HttpResponse update(ProjectUpdateForm form) {
      Project project = BeanUtil.createAndCopy(Project.class, form);

      UniversalDao.update(project);

      return new HttpResponse(HttpResponse.Status.OK.getStatusCode());
  }
Key points of this implementation
  • The @Path and @PUT annotations are used to define the business action methods to be mapped on PUT requests.