Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3630

Parameter value [\] did not match expected type [java.lang.Long (n/a)]

$
0
0

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");        }    }}@PutMapping("/update")public ResponseEntity<String> 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.");    }}

RecordRepository

@Repositorypublic interface RecordRepository extends JpaRepository<Record, Long> {Record findByNumber(long number);boolean existsByNumberAndDataLike(long number, String data);boolean existsByNumberAndDataNotLike(long number, String data);

}

Entity

@Entitypublic class Record {private long id;@Idprivate long number;private String data;private String enabled;//getters and setters

I am trying to update records with conditions:

  1. if a number exists and data exists it will throw the "RecordAlreadyExistsException" exception.
  2. if a number exists and data for it is absent it will add the new data.
  3. if both are absent it will throw the "NumberNotPresentException" exception.

So, 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 type conversion of string to long for the "number" arguement but I am entering a numeric value and it works on first try but from second call it throws the exception.


Viewing all articles
Browse latest Browse all 3630

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>