4

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;
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
gg ff
  • 489
  • 1
  • 10
  • 20

4 Answers4

6

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).

Michael
  • 629
  • 6
  • 15
2

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);
}
Christos Karapapas
  • 1,018
  • 3
  • 19
  • 40
2

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.

Miraj Hamid
  • 554
  • 10
  • 19
2

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")
hexhad
  • 1,139
  • 12
  • 14