Could not set field value [] value by reflection after creating a rest controller

Viewed 28

I'm calling save method of hibernate from a rest controller using service layer

rest controller -> service layer -> repository layer //not working

main(a temporary class to check service layer) //working

the auto generated value of player class is not working, when I call it from controller. but working perfectly form main method.

@RestController
@RequestMapping("/player")
public class PlayerRest {
private PlayerService playerService;

@Autowired
public PlayerRest(PlayerService ps) {
    System.out.println("arg0");
    playerService = ps;
}

@PostMapping("/save")
Player savePlayer(@RequestBody PlayerRequest entity) {
    Player ent = new Player(entity.name, entity.tag, entity.total, entity.paid);
    if (playerService.persist(ent)) { //returns true when inserted an entity
        return ent;
    } else {
        return null;
    }
}
}

the above code gives the error Could not set field value [1] value by reflection

but runs perfectly and stores values perfectly by following code

public class Main {

    public static void main(String[] args) {

        PlayerService playerService = new PlayerService();
        
        
        Player p1 = new Player("x", "p", 100, 50);
        Player p2 = new Player("y", "a", 200, 50);

        playerService.persist(p1);
        playerService.persist(p2);// works perfectly, even with auto generated value

    }

}
0 Answers
Related