nested exception is java.lang.IllegalArgumentException: Parameter value [/] did not match expected type [java.lang.Long (n/a)]

nested exception is java.lang.IllegalArgumentException: Parameter value [/] did not match expected type [java.lang.Long (n/a)]


This error message indicated that there’s a mismatch between the type of a parameter value and the expected type in a method or query. This issue occurs in frameworks like Spring or Hibernate when trying to execute queries or methods with parameters of incorrect types. Suppose you have a method or query that retrieves data based on a user’s completion status, and it expects the completion status to be provided as a Long representing an ID. However, somewhere in your code, you mistakenly pass an instance of String instead of a Long.

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List<User> getUserByCompletionStatusId(Long completionStatusId) {
        return userRepository.findByCompletionStatusId(completionStatusId);
    }
}
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/userData")
    public ResponseEntity<List<User>> getDataByCompletionStatusId(@RequestParam("completionStatusId") String completionStatusId) {
        List<User> dataList = userService.getDataByCompletionStatusId(completionStatusId);
        return ResponseEntity.ok(dataList);
    }
}

In this scenario, when the controller method is called, Spring attempts to convert the String parameter to a Long ID, as specified in the method signature. However, since it is of type String, Spring encounters a type mismatch error and throws an IllegalArgumentException.
To resolve this issue, you should review the method or query where the error occurred and ensure that the parameter types match what is expected. If CompletionStatus is not supposed to be used as a parameter, you may need to revise the code to provide the correct type of parameter, likely a Long. If CompletionStatus is indeed intended as a parameter, you may need to update the method or query to handle it appropriately, possibly by converting the necessary CompletionStatus value to long.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *