DIGIT CORE
Search…
Building the Web Layer
Implementing the controller layer in Spring
The web/controller layer handles all the incoming REST requests to a service.
Also @RequestMapping("/v1") annotation should be added on top of the controller class. It will contain the version of the API(This will become part of the API endpoint url)
Any request handler in the controller layer is going to have the following sequence of execution -
  1. 1.
    Making a call to the method in the Service layer and getting the response back from it.
  2. 2.
    Building responseInfo.
  3. 3.
    Building final response to be returned to the client.
Sample request handler in controller layer
For this guide, our controller class will contain the following content -
package digit.web.controllers;
import com.fasterxml.jackson.databind.ObjectMapper;
import digit.service.BirthRegistrationService;
import digit.utils.ResponseInfoFactory;
import digit.web.models.*;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Tag;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.egov.common.contract.response.ResponseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
@javax.annotation.Generated(value = "org.egov.codegen.SpringBootCodegen", date = "2022-07-26T12:39:05.988+05:30")
@Slf4j
@ToString
@Controller
@RequestMapping("/birth-services")
public class V1ApiController{
private final ObjectMapper objectMapper;
private final HttpServletRequest request;
private BirthRegistrationService birthRegistrationService;
@Autowired
private ResponseInfoFactory responseInfoFactory;
@Autowired
public V1ApiController(ObjectMapper objectMapper, HttpServletRequest request, BirthRegistrationService birthRegistrationService) {
this.objectMapper = objectMapper;
this.request = request;
this.birthRegistrationService = birthRegistrationService;
}
@RequestMapping(value="/v1/registration/_create", method = RequestMethod.POST)
public ResponseEntity<BirthRegistrationResponse> v1RegistrationCreatePost(@ApiParam(value = "Details for the new Birth Registration Application(s) + RequestInfo meta data." ,required=true ) @Valid @RequestBody BirthRegistrationRequest birthRegistrationRequest) {
List<BirthRegistrationApplication> applications = birthRegistrationService.registerBtRequest(birthRegistrationRequest);
ResponseInfo responseInfo = responseInfoFactory.createResponseInfoFromRequestInfo(birthRegistrationRequest.getRequestInfo(), true);
BirthRegistrationResponse response = BirthRegistrationResponse.builder().birthRegistrationApplications(applications).responseInfo(responseInfo).build();
return new ResponseEntity<>(response, HttpStatus.OK);
}
@RequestMapping(value="/v1/registration/_search", method = RequestMethod.POST)
public ResponseEntity<BirthRegistrationResponse> v1RegistrationSearchPost(@RequestBody RequestInfoWrapper requestInfoWrapper, @Valid @ModelAttribute BirthApplicationSearchCriteria birthApplicationSearchCriteria) {
List<BirthRegistrationApplication> applications = birthRegistrationService.searchBtApplications(requestInfoWrapper.getRequestInfo(), birthApplicationSearchCriteria);
ResponseInfo responseInfo = responseInfoFactory.createResponseInfoFromRequestInfo(requestInfoWrapper.getRequestInfo(), true);
BirthRegistrationResponse response = BirthRegistrationResponse.builder().birthRegistrationApplications(applications).responseInfo(responseInfo).build();
return new ResponseEntity<>(response,HttpStatus.OK);
}
@RequestMapping(value="/v1/registration/_update", method = RequestMethod.POST)
public ResponseEntity<BirthRegistrationResponse> v1RegistrationUpdatePost(@ApiParam(value = "Details for the new (s) + RequestInfo meta data." ,required=true ) @Valid @RequestBody BirthRegistrationRequest birthRegistrationRequest) {
BirthRegistrationApplication application = birthRegistrationService.updateBtApplication(birthRegistrationRequest);
ResponseInfo responseInfo = responseInfoFactory.createResponseInfoFromRequestInfo(birthRegistrationRequest.getRequestInfo(), true);
BirthRegistrationResponse response = BirthRegistrationResponse.builder().birthRegistrationApplications(Collections.singletonList(application)).responseInfo(responseInfo).build();
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
*** NOTE: At this point, your IDE must be showing a lot of errors but do not worry we will add all dependent layers as we progress through this guide and the errors will go away.
Since the Codegen jar creates the search API with search parameters annotated with @RequestParam rather than taking request parameters as a POJO. For this, we will create a POJO by the name of BirthApplicationSearchCriteria under models package. Put the following content in the POJO -
package digit.web.models;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import java.util.List;
@Data
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class BirthApplicationSearchCriteria {
@JsonProperty("tenantId")
private String tenantId;
@JsonProperty("status")
private String status;
@JsonProperty("ids")
private List<String> ids;
@JsonProperty("applicationNumber")
private String applicationNumber;
}
All content on this page by eGov Foundation is licensed under a Creative Commons Attribution 4.0 International License.
Copy link