All content on this page by eGov Foundation is licensed under a Creative Commons Attribution 4.0 International License.
Overview
The web/controller layer handles all the incoming REST requests to a service.
Steps
The @RequestMapping("/v1") annotation is added on top of the controller class. This contains the version of the API (this becomes a part of the API endpoint URL).
Steps to setup the request handler in the controller layer
Follow the steps below to set up the request handler in the controller layer.
Make a call to the method in the Service Layer and get the response back from it.
Build the responseInfo.
Build the final response to be returned to the client.
The controller class here contains the below content -
packagedigit.web.controllers;importcom.fasterxml.jackson.databind.ObjectMapper;importdigit.service.BirthRegistrationService;importdigit.utils.ResponseInfoFactory;importdigit.web.models.*;importio.swagger.annotations.ApiParam;importio.swagger.annotations.Tag;importlombok.ToString;importlombok.extern.slf4j.Slf4j;importorg.egov.common.contract.response.ResponseInfo;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.http.HttpStatus;importorg.springframework.http.ResponseEntity;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.ModelAttribute;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importjavax.servlet.http.HttpServletRequest;importjavax.validation.Valid;importjava.util.Collections;importjava.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")publicclassV1ApiController{privatefinalObjectMapper objectMapper;privatefinalHttpServletRequest request;privateBirthRegistrationService birthRegistrationService; @AutowiredprivateResponseInfoFactory responseInfoFactory; @AutowiredpublicV1ApiController(ObjectMapper objectMapper,HttpServletRequest request,BirthRegistrationService birthRegistrationService) {this.objectMapper= objectMapper;this.request= request;this.birthRegistrationService= birthRegistrationService; } @RequestMapping(value="/v1/registration/_create", method =RequestMethod.POST)publicResponseEntity<BirthRegistrationResponse> v1RegistrationCreatePost(@ApiParam(value ="Details for the new Birth Registration Application(s) + RequestInfo meta data.",required=true ) @Valid @RequestBodyBirthRegistrationRequest birthRegistrationRequest) {List<BirthRegistrationApplication> applications =birthRegistrationService.registerBtRequest(birthRegistrationRequest);ResponseInfo responseInfo =responseInfoFactory.createResponseInfoFromRequestInfo(birthRegistrationRequest.getRequestInfo(),true);BirthRegistrationResponse response =BirthRegistrationResponse.builder().birthRegistrationApplications(applications).responseInfo(responseInfo).build();returnnewResponseEntity<>(response,HttpStatus.OK); } @RequestMapping(value="/v1/registration/_search", method =RequestMethod.POST)publicResponseEntity<BirthRegistrationResponse> v1RegistrationSearchPost(@RequestBodyRequestInfoWrapper requestInfoWrapper, @Valid @ModelAttributeBirthApplicationSearchCriteria 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();returnnewResponseEntity<>(response,HttpStatus.OK); } @RequestMapping(value="/v1/registration/_update", method =RequestMethod.POST)publicResponseEntity<BirthRegistrationResponse> v1RegistrationUpdatePost(@ApiParam(value ="Details for the new (s) + RequestInfo meta data.",required=true ) @Valid @RequestBodyBirthRegistrationRequest birthRegistrationRequest) {BirthRegistrationApplication application =birthRegistrationService.updateBtApplication(birthRegistrationRequest);ResponseInfo responseInfo =responseInfoFactory.createResponseInfoFromRequestInfo(birthRegistrationRequest.getRequestInfo(),true);BirthRegistrationResponse response =BirthRegistrationResponse.builder().birthRegistrationApplications(Collections.singletonList(application)).responseInfo(responseInfo).build();returnnewResponseEntity<>(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 within the Models package. Insert the following content in POJO.