小编典典

Spring Hibernate没有得到参数的值

spring-boot

我尝试post通过邮递员用该方法对Spring Boot应用程序进行API调用。这是输入:

{
  "username": "name",
  "password": "1234",
  "age": 12,
  "salary": 5000,
  "role": 1
}

这是控制器中的代码:

@RequestMapping(value = "/signup", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> saveUser(@RequestBody UserDto user) {
        try {
            System.out.println(user.getUsername()); // => name
            System.out.println(user.getPassword()); // => 1234
            System.out.println(user.getSalary()); // => 5000
            System.out.println(user.getRole()); // => 1
            System.out.println(user.getAge()); // => 12
            userService.save(user);
            return ResponseEntity.ok().body("insert done");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

这是我的User.java

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String username;
@Column
@JsonIgnore
private String password;
@Column
private long salary;
@Column
private int age;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "role_id")
private Role role;
// getters and setters

这是我的Role.java

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "role_name", nullable = false)
private String roleName;
// getters and setters

这是我的UserDto.java

private String username;
private String password;
private int age;
private long salary;
private int role;
// getters and setters

这是我的RoleDto.java

private Long id;
private String roleName;
// getters and setters

问题 :它引发错误'Column 'role_id' cannot be null'

这是在userServiceImpl中:在(BeanUtils)行下面是here

    @Override
    public User save(UserDto user) throws Exception {
        User newUser = new User();
        BeanUtils.copyProperties(user, newUser, "password");
        newUser.setPassword(bcryptEncoder.encode(user.getPassword()));
        try {
            userDao.save(newUser);
        } catch (Exception e) {
            throw new Exception(e);
            // this throw "Column role_id cannot be null"
        }
        return newUser;
    }

但是,如果使用下面的代码,则该setRole方法是not applicable因为我user.getRole()在DTO中int定义为,而newUser.setRole()User实体中定义为Role。但是,如果我将setRolein
User实体更改为int,那么如何告诉Spring 和ManyToOne之间的关系?User``Role

@Override
public User save(UserDto user) throws Exception {
    User newUser = new User();
    newUser.setUsername(user.getUsername());
    newUser.setPassword(bcryptEncoder.encode(user.getPassword()));
    newUser.setAge(user.getAge());
    newUser.setSalary(user.getSalary());
    newUser.setRole(user.getRole()); // here is the problem
    try {
        userDao.save(newUser);
    } catch (Exception e) {
        throw new Exception(e);
    }
    return newUser;
}

注:我想我只是想为输入参数role是只是一个integer如上规定。


阅读 250

收藏
2020-05-30

共1个答案

小编典典

由于您在请求中获得的角色为1,因此您应该执行以下操作:

Role role = new Role();
role.setId(user.getRole());
role.setRoleName("some name");
newUser.setRole(role);

在代码的第一部分中,问题可能出在这里BeanUtils.copyProperties(user, newUser, "password");,如果您尝试调试或打印newUser该行之后,您会看到BeanUtils无法正确填充newUser对象的所有字段。这是因为在目标(newUser)中,该字段role的类型为Role,而在源(用户)中,该字段的类型为int。

2020-05-30