Skip to content
Advertisement

How to send headers from controller into another class in Java

I have a controller like below

SubmitBatchController.java

@RestController
@RequestMapping({"/"})
@Api(value = "batch", tags = {"Batch "}, authorizations = {@Authorization(value="JwtToken")})
public class SubmitBatchController extends BasicController {

@PostMapping(value = "v1/batch", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public CommonResponse<BatchSubmissionResponseModel> submitBatchClaim(
@ApiParam(hidden = true) @RequestHeader(required = false) String transId,
@ApiParam(hidden = true) @RequestHeader(required = false) String paymentReleaseInd,
@ApiParam(hidden = true) @RequestHeader(required = false) String emailMatchedInd,
@ApiParam(hidden = true) @RequestHeader(vrequired = false) String role,
@RequestBody BatchSubmissionRequestModel batchSubmissionRequestModel,
BindingResult br, HttpServletRequest request, HttpServletResponse response) throws Exception {

LocalDateTime startLocaleDateTime = LocalDateTime.now();
BatchSubmissionResponseModel batchSubmissionResponseModel = new BatchSubmissionResponseModel();
ContextBase ctxBase = getChainBaseContext(request);
ctxBase.put(PARAM_LOC.RESPONSE, batchSubmissionResponseModel);
HeaderRequestModel headers = new headerRequestModel();  
batchSubmissionRequestModel.setTransId(transId);    
headers.setRole(role);
headers.setPaymentReleaseInd(paymentReleaseInd);
headers.setEmailMatchedInd(emailMatchedInd);

batchSubmissionRequestModel.setHeaderRequestModel(headers);
}
} 

       

BatchSubmissionRequestModel.java

public class BatchSubmissionRequestModel {

@ApiModelProperty(hidden = true) // this is captured as a header and set in the controller
@NotBlank(message = "Headers.transId.NotBlank")
private String transId;
@ApiModelProperty(hidden = true)
private HeaderRequestModel headerRequestModel;

public String getTransId() {
return transId;
}
public void setTransId(String transId) {
this.transId = transId;
}
public HeaderRequestModel getHeaderRequestModel() {
return headerRequestModel;
}
public void setHeaderRequestModel(HeaderRequestModel headerRequestModel) {
this.headerRequestModel= headerRequestModel;
}
}

HeaderRequestModel.java

public class HeaderRequestModel {     
    private String paymentReleaseInd;
    private String emailMatchedInd;
    private String role;
    public String getPaymentReleaseInd() {
        return paymentReleaseInd;
    }
    public void setPaymentReleaseInd(String paymentReleaseInd) {
        this.paymentReleaseInd = paymentReleaseInd;
    }
    public String getEmailMatchedInd() {
        return emailMatchedInd;
    }
    public void setEmailMatchedInd(String emailMatchedInd) {
        this.emailMatchedInd = emailMatchedInd;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
}

Now I have another class, which has to take the headers from SubmitBatchController.java & set it into FullEligibilityService.java

FullEligibilityRequestModel.java

public class FullEligibilityRequestModel{
private String transId;
private HeaderRequestModel headerRequestModel;

public String getTransId() {
return transId;
}
public void setTransId(String transId) {
this.transId = transId;
}

public DapHeaderRequestModel getDapHeaderRequestModel() {
return dapHeaderRequestModel;
}
public void setDapHeaderRequestModel(DapHeaderRequestModel dapHeaderRequestModel) {
this.dapHeaderRequestModel = dapHeaderRequestModel;
}
}

FullEligibilityService.java

(request is mapped to FullEligibilityRequestModel)

@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class FullEligibilityServiceextends extends AServiceTask <FullEligibilityRequestModel, FullEligibilityResponseModel>{

private static final String HEADER_FULL_ELIGIBILITY_TRANS_ID = "transId";
private static final String HEADER_PAYMENT_RELEASE_INDICAATOR =  "paymentReleaseInd"; 
private static final String HEADER_EMAIL_MATCHED_INDICATOR = "emailMatchedInd";
private static final String HEADER_ROLE = "role";

@Override
protected void processTask() {
try {

Map<String, String> headers = new HashMap<String, String>();
headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
headers.put(HEADER_FULL_ELIGIBILITY_TRANS_ID, request.getTransId());
headers.put(HEADER_PAYMENT_RELEASE_INDICAATOR, request.getHeaderRequestModel().getPaymentReleaseInd()); // getting NULL
headers.put(HEADER_EMAIL_MATCHED_INDICATOR, request.getHeaderRequestModel().getEmailMatchedInd()); // getting NULL
headers.put(HEADER_DAP_ROLE, request.getHeaderRequestModel().getRole()); // getting NULL
.....
}
catch (Exception e) {
LOGGER.error("FullEligibilityService.processTask call failed");
exception = e;
status = STATUS_TASK_EXCEPTION;
e.printStackTrace();
}
}

I’m not able to get the headers set in SubmitBatchController.java into FullEligibilityService.java, due to which I get null values.

Can someone help me how to set the PaymentReleaseInd, EmailMatchedInd, HeaderRequestModel set in SubmitBatchController.java into FullEligibilityService.java

Advertisement

Answer

You need to inject an instance of FullEligibilityService into SubmitBatchController as follows and then you just need to call the corresponding method with the required parameters:

@RestController
@RequestMapping({"/"})
@Api(value = "batch", tags = {"Batch "}, authorizations = {@Authorization(value="JwtToken")})
public class SubmitBatchController extends BasicController {

    @Autowired
    FullEligibilityService fullEligibilityService;

    @PostMapping(value = "v1/batch", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public CommonResponse<BatchSubmissionResponseModel> submitBatchClaim(
      @ApiParam(hidden = true) @RequestHeader(required = false) String transId,
      @ApiParam(hidden = true) @RequestHeader(required = false) String paymentReleaseInd,
      @ApiParam(hidden = true) @RequestHeader(required = false) String emailMatchedInd,
      @ApiParam(hidden = true) @RequestHeader(vrequired = false) String role,
      @RequestBody BatchSubmissionRequestModel batchSubmissionRequestModel,
      BindingResult br, HttpServletRequest request, HttpServletResponse response) throws Exception {

          LocalDateTime startLocaleDateTime = LocalDateTime.now();
          BatchSubmissionResponseModel batchSubmissionResponseModel = new BatchSubmissionResponseModel();
          ContextBase ctxBase = getChainBaseContext(request);
          ctxBase.put(PARAM_LOC.RESPONSE, batchSubmissionResponseModel);
          HeaderRequestModel headers = new headerRequestModel();  
          batchSubmissionRequestModel.setTransId(transId);    
          headers.setRole(role);
          headers.setPaymentReleaseInd(paymentReleaseInd);
          headers.setEmailMatchedInd(emailMatchedInd);

          fullEligibilityService.processTask(paymentReleaseInd, emailMatchedInd, headers);
    }

   (...)
}

You need to change Service to accept the data you need as follows:

@Service
public class FullEligibilityServiceextends extends AServiceTask <FullEligibilityRequestModel, FullEligibilityResponseModel>{

public void processTask(String paymentReleaseInd, String emailMatchedInd, HeaderRequestModel headers) {
    (...) //Whatever logic you want to implement
}

Right now you are trying to pass around the data you need via request headers which is definitely a bad practice.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement