Spring Hibernate没有得到参数的值 [英] spring hibernate did not get the value of parameter

查看:60
本文介绍了Spring Hibernate没有得到参数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过邮递员使用 post 方法对我的spring boot应用程序进行API调用。
这是输入:

I try to make an API call with the post method via postman to my spring boot application. Here is the input:

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

以下是控制器中的代码:

Here is the code in the controller:

@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

Here is my 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

Here is my 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

Here is my UserDto.java

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

这是我的RoleDto.java

Here is my RoleDto.java

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

问题:它会引发错误'列'role_id'不能为空''

此处位于userServiceImpl中:
BeanUtils )是指此处

Here is in the userServiceImpl: Below line (BeanUtils) is refer to 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 方法不适用,因为我在DTO中将 user.getRole()定义为 int ,但在 User 实体中定义 newUser.setRole()作为角色。但是,如果我将 User 实体中的 setRole 更改为 int ,那么,我怎么能告诉春天关于 User Role <的 ManyToOne 关系/ code>?

But, if I use below code, the setRole method is not applicable since I define user.getRole() in the DTO as int but define newUser.setRole() in the User entity as Role. But if I change the setRole in User entity as int, then, how I can tell the spring about the ManyToOne relation between User and 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;
}

注意:我想我只想为<$ c做输入参数$ c>角色只是上面提供的整数

Note: I think I just want to make the input parameter for role to be just an integer as provided above.

推荐答案

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

Since you are receiving 1 for the role in your request you should do something like:

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

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

In the first part of the code the problem may be here BeanUtils.copyProperties(user, newUser, "password");, if you try to debug or print newUser after that line you will see that BeanUtils was not able to populate correctly all the fields of your newUser object. And this is because in the destination (newUser) the field role is of type Role, while in the source (user) the type of that field is just int.

这篇关于Spring Hibernate没有得到参数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆