Below is code from my service class and controller class
public void updateRecord(long number, String data) throws RecordAlreadyExistsException, NumberNotPresentException {
boolean recordExists = recordRepository.existsByNumberAndDataLike(number, data);
if (recordExists) {
throw new RecordAlreadyExistsException(“Entered number and data is present”);
} else {
boolean numberExists = recordRepository.existsByNumberAndDataNotLike(number, data);
if (numberExists) {
Record record = recordRepository.findByNumber(number);
String d = record.getData();
record.setData(d + “;” + data);
recordRepository.save(record);
} else {
throw new NumberNotPresentException(“Entered number is not present”);
}
}
your text
@PutMapping(“/update”)
public ResponseEntity updateRecord(@RequestBody Map<String, String> payload) {
try {
String data = payload.get(“data”);
long number = Long.parseLong(payload.get(“number”));
service.updateRecord(number, data);
return ResponseEntity.ok(“data added successfully.”);
} catch (RecordAlreadyExistsException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
} catch (NumberNotPresentException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
} catch (NumberFormatException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(“Invalid format.”);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(“An unexpected error occurred while processing the request.”);
}
}
When I make the first api call the code works as expected but on the second call I get the error:Parameter value [] did not match expected type [java.lang.Long (n/a)].
I tried searching for the error, it is related to conversion of string to long for the “number” arguement but my input is numeric value and it works on first try but from second call it throws the exception.