Build The Web Layer
Implementing the controller layer in Spring
Overview
Steps
Steps to setup the request handler in the controller layer

Last updated
Was this helpful?
Implementing the controller layer in Spring

Last updated
Was this helpful?
Was this helpful?
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);
}
}
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;
}