Parameter value [1] did not match expected type [java.lang.Integer (n/a)]
Solution 1
Since the Id is a uuid you must keep it as a string in the entity.
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;
Therefore you are bound to use it as a String in Service and in Controller.
@GetMapping("/createCharacter")
public Character createCharacters(@RequestParam("userId") String userId, @RequestParam("mapId") long mapId) {
return createCharactersService.createCharacters(userId, mapId);
}
Solution 2
For those who find this issue based on the error:
java.lang.IllegalArgumentException: Parameter value [Type@abdc80fc] did not match expected type [Type (n/a)]
You might be using JPA like this:
@Repository
public interface OtherRepository extends JpaRepository<Other, Long> {
List<Other> findAllByType(final Type type);
In that case, please make use of the id of Type (thus: findAllByTypeId
).
Solution 3
If you are using JPA with @Query for example :
@Query("SELECT r FROM Employee r WHERE r.empId = ?1)
Employee getMyEmployee(long id);
OR
@Query("SELECT r FROM Employee r WHERE r.empId = :id)
Employee getMyEmployee(@Param("id") long id);
Make sure the passing parameter is long value and empId also a long value. If you execute the method
classObj.getMyEmployee(someIntVariable)
by passing int value it could cause the above error.
Solution 4
in my case I have used DTOs so
Previously my @query was like this
@query("SELECT ... WHERE ucs.district = ?1")
Then I changed @query like this
@query("SELECT ... WHERE ucs.district.id = ?1")
Related videos on Youtube
gg ff
Updated on June 05, 2021Comments
-
gg ff almost 2 years
I have entity and rest controller, when I make a request to my controller it throws this exception:
java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [java.lang.Integer (n/a)]
My controller:
@GetMapping("/createCharacter") public Character createCharacters(@RequestParam("userId") Integer userId, @RequestParam("mapId") long mapId) { return createCharactersService.createCharacters(userId, mapId); }
My entity has int type id:
@Id @GeneratedValue(strategy = GenerationType.AUTO) private int id;
-
Christos Karapapas about 4 yearsWhat exactly are you trying to accomplish?
-
gg ff about 4 yearsI tried to change Integer to Long or String it helped to fix my problem but in this case my Spring Data Rest for user entity doesn't work.
-
Christos Karapapas about 4 yearsWhy would you declare an Id as a String, is it a uuid? Usually Ids are of type Long in the entity, especially when the the relative id field in database is Big Integer.
-
gg ff about 4 yearsYe, it is UUID. But now i replaced it with primitive numbers.
-