Rest方法无法获得正确的数据

时间:2018-05-30 11:43:34

标签: java rest spring-boot curl restful-architecture

我有一个基本的SpringBoot应用程序。使用Spring Initializer,JPA,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件。我已经定义了这个Rest方法来更新用户

@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);

    }

我使用

从控制台调用此方法
curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJyaWNhcmQub2xsZUBnbWFpbC5jb20iLCJleHAiOjE1MjgxMTM3NTIsImlhdCI6MTUyNzUwODk1Mn0.QdxabtU1U87pYvyTstT1EG3E6uVpLo2mXCF0FF8iD6acKoAXKl_A0-eV_GrpOFg5FF1qR6B7llI5_USJL85YTQ" -d '{
\"id\":1,\"username\":\"pere.peris@gmail.com\",\"email\":\"pere.peris@gmail.com\",\"firstName\":\"Pere\",\"lastName\":\"PERIS\",\"country\":\"CAT\",\"enabled\":true}' "http://127.0.0.1:2233/iCrypts/api/users/1"

但是在System.out.println(user);我从POJO得到的所有值都是空的(!?),但是id是1

1 个答案:

答案 0 :(得分:1)

缺少

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

id

}

它包含值为1的{​​{1}}的原因是它必须在持久层自动生成。