Source:
public ResponseEntity<?> createSample(@RequestBody SampleDto sampleDto, UriComponentsBuilder b) throws IOException { if (sampleDto == null) { log.error("Received null sampleDto from front end; cannot convert to Sample. Something likely went wrong in the JS DTO conversion."); throw new RestException("Cannot convert null to Sample", Status.BAD_REQUEST); } Long id = null; try { if (sampleDto instanceof SampleAliquotDto) { SampleAliquotDto dto = (SampleAliquotDto) sampleDto; if (dto.getParentId() != null) { } else if (dto.getSampleClassId() == null) { throw new IllegalStateException("No parent and no target sample class."); } else { SampleClass sampleClass = sampleClassService.get(dto.getSampleClassId()); if (!sampleClass.getSampleCategory().equals(SampleAliquot.CATEGORY_NAME)) { throw new IllegalStateException("Class and type mismatch."); } SampleClass stockClass = sampleClassService.inferStockFromAliquot(sampleClass); dto.setStockClassId(stockClass.getId()); } } Sample sample = Dtos.to(sampleDto); id = sampleService.create(sample); } catch (ConstraintViolationException | IllegalArgumentException e) { log.error("Error while creating sample. ", e); RestException restException = new RestException(e.getMessage(), Status.BAD_REQUEST); if (e instanceof ConstraintViolationException) { restException.addData("constraintName", ((ConstraintViolationException) e).getConstraintName()); } throw restException; } UriComponents uriComponents = b.path("/sample/{id}").buildAndExpand(id); HttpHeaders headers = new HttpHeaders(); headers.setLocation(uriComponents.toUri()); return new ResponseEntity<>(headers, HttpStatus.CREATED); }

Target:
public ResponseEntity<?> createSample(@RequestBody SampleDto sampleDto, UriComponentsBuilder b) throws IOException { if (sampleDto == null) { log.error("Received null sampleDto from front end; cannot convert to Sample. Something likely went wrong in the JS DTO conversion."); throw new RestException("Cannot convert null to Sample", Status.BAD_REQUEST); } Long id = null; try { if (sampleDto instanceof SampleAliquotDto) { SampleAliquotDto dto = (SampleAliquotDto) sampleDto; if (dto.getParentId() != null) { } else if (dto.getSampleClassId() == null) { throw new RestException("No parent and no target sample class.", Status.BAD_REQUEST); } else { SampleClass sampleClass = sampleClassService.get(dto.getSampleClassId()); if (sampleClass == null) { throw new RestException("Cannot find sample class: " + dto.getSampleClassId(), Status.BAD_REQUEST); } if (!sampleClass.getSampleCategory().equals(SampleAliquot.CATEGORY_NAME)) { throw new RestException("Class and type mismatch.", Status.BAD_REQUEST); } SampleClass stockClass = sampleClassService.inferStockFromAliquot(sampleClass); dto.setStockClassId(stockClass.getId()); } } Sample sample = Dtos.to(sampleDto); id = sampleService.create(sample); } catch (ConstraintViolationException | IllegalArgumentException e) { log.error("Error while creating sample. ", e); RestException restException = new RestException(e.getMessage(), Status.BAD_REQUEST); if (e instanceof ConstraintViolationException) { restException.addData("constraintName", ((ConstraintViolationException) e).getConstraintName()); } throw restException; } UriComponents uriComponents = b.path("/sample/{id}").buildAndExpand(id); HttpHeaders headers = new HttpHeaders(); headers.setLocation(uriComponents.toUri()); return new ResponseEntity<>(headers, HttpStatus.CREATED); }