I have the following REST endpoint:
@PostMapping(path = "/dispatchJob") public ResponseEntity<Void> dispatchJob(@RequestBody(required = false) JobDispatchRequestBody dispatchRequestBody) throws FailedToAcceptJobException { modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull()); Job j = modelMapper.map(dispatchRequestBody.getDispatchRequestsDTO(), Job.class); jobsSvc.assignJob(j, dispatchRequestBody.getDispatchEpoch()); return ResponseEntity.noContent().build(); }The "jobsSvc.assignJob" function throws exception:
@Override public synchronized void assignJob(Job j, Long dispatchEpoch) throws FailedToAcceptJobException { initJobObject(j); throw new FailedToAcceptJobException(new DispatchJobErrorResponse("System is shutting down, not accepting new job!", DispatchJobErrorResponse.JobRejectionErrorCode.SystemISShuttingDown));}And the exception class is as follows:
@Datapublic class FailedToAcceptJobException extends Exception { private final DispatchJobErrorResponse dispatchJobErrorResponse; public FailedToAcceptJobException(DispatchJobErrorResponse dispatchJobErrorResponse) { super("Failed to assign job on agent exception: " + dispatchJobErrorResponse.getMessage()); this.dispatchJobErrorResponse = dispatchJobErrorResponse; }I created the following handler that is being called once exception is thrown within this endpoint (I validated that through test):
@ExceptionHandler(value = FailedToAcceptJobException.class) public ResponseEntity<Object> handleCustomException(HttpServletRequest request, FailedToAcceptJobException ex) { return new ResponseEntity<>(ex.getDispatchJobErrorResponse(),HttpStatus.BAD_REQUEST); }My issue is, when I'm trying to call this endpoint via post request, I don't get the body and I can't access the error object (this is a test).
String url = "/dispatchJob";JobDispatchDTO jobDispatchDTO = new JobDispatchDTO();JobDispatchRequestBody dispatchRequestBody = new JobDispatchRequestBody(jobDispatchDTO,1L);ResponseEntity<Void> res = restTemplate.postForEntity(url, dispatchRequestBody,Void.class);I've been playing with it for hours, I realize that eventually I'll have to use object mapper in order to access "DispatchJobErrorResponse" fields, but the body remains null in the response entity no matter what I do.
Please assist