JSON 解析错误:无法构造实例:没有 int/Int 参数构造函数/工厂方法来从数值反序列化

问题描述 投票:0回答:1

我正在尝试使用mysql数据库插入用户记录

客户.java

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstname;
    private String lastname;
    @Column(unique = true)
    private String email;
    @Column(nullable = false)
    private String password;
    @Column(unique = true, nullable = false)
    private String username;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_roles",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id"))
    private List<Role> roles;
}

客户控制器

    @PostMapping("/customer")
    public void addCustomer(@RequestBody Customer customer) {
        customerService.createCustomer(customer);
    }

客户服务

@Service
public class CustomerService {
    public CustomerService(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    public void createCustomer(Customer customer) {

        customerRepository.save(customer);
    }
}

角色.java

public class Role {

    @Id
    private Long id;

    private String name;

    @ManyToMany(mappedBy = "roles")
    private Set<Customer> customers;
}

JSON 对象

{
  "firstname": "John",
  "lastname": "Doe",
  "email": "[email protected]",
  "password": "john",  // Ensure that passwords are hashed and never stored in plain text
  "username": "john123",
  "roles" : [1]
}
java json spring-boot serialization json-deserialization
1个回答
0
投票

共享的JSON对象似乎不正确,请尝试使用以下

{
    "firstname": "John",
    "lastname": "Doe",
    "email": "[email protected]",
    "password": "john",
    "username": "john123",
    "roles": [
        {
            "id": 1
        }
    ]
}

由于角色是一个复杂的对象,因此您需要将其包含在

{}
中。

希望这有帮助!

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.