Search code examples
javarestspring-bootcurlrestful-architecture

Rest method does not get the proper data


I have a basic SpringBoot app. using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. I've defined this Rest method to update a User

@PutMapping(path = "/api/users/{id}", 
                        consumes = "application/json", 
                        produces = "application/json")
    public ResponseEntity<User> updateUser
                                    (HttpServletRequest request, 
                                    @PathVariable long id,
                                    User user) {

        System.out.println(user);
        saveUser (user)
        return ResponseEntity.ok(user);

    }

I call this method from the console, using

curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJyaWNhcmQub2xsZUBnbWFpbC5jb20iLCJleHAiOjE1MjgxMTM3NTIsImlhdCI6MTUyNzUwODk1Mn0.QdxabtU1U87pYvyTstT1EG3E6uVpLo2mXCF0FF8iD6acKoAXKl_A0-eV_GrpOFg5FF1qR6B7llI5_USJL85YTQ" -d '{
\"id\":1,\"username\":\"[email protected]\",\"email\":\"[email protected]\",\"firstName\":\"Pere\",\"lastName\":\"PERIS\",\"country\":\"CAT\",\"enabled\":true}' "http://127.0.0.1:2233/iCrypts/api/users/1"

But on the System.out.println(user); all the values I get from the POJO are null (!?), but the id that is 1


Solution

  • @RequestBody annotation is missing

    public ResponseEntity<User> updateUser
                                    (HttpServletRequest request, 
                                    @PathVariable long id,
                                    @RequestBody User user) {
    ......
    

    }

    THe reason it contains id with value 1 is it must be getting auto-generated at persistance layer.