spring-data-jpa Java SpringBoot POST到Postgresql会导致空值

hvvq6cgz  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(101)

我试图构建一个简单的Web应用程序,其中包含三个相连的PostgreSQL表和管理它们的REST API。但是,当我尝试通过POST添加新客户时,所有列都被插入为NULL。

客户.java

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@NonNull
@Table(name = "customers")
@Entity
public class Customer implements Serializable {
    private @Id @Column(name = "customerid")
    @GeneratedValue(strategy = GenerationType.IDENTITY) Long id;
    @Column(name = "customer_name")
    private String customerName;
    @Column(name = "customer_surname")
    private String customerSurname;
}

客户信息库.java

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}

IC客户服务.java

public interface ICustomerService {
    List<Customer> getAll();
    Customer getByID(Long id);
    Long add(Customer c);
    void remove(Long id);
}

客户服务.java

@Service
public class CustomerService implements ICustomerService {
    private CustomerRepository customerRepository;
    @Autowired
    public CustomerService(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }
    @Override
    public List<Customer> getAll() {
        return this.customerRepository.findAll();
    }
    @Override
    public Customer getByID(Long id) {
        return this.customerRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(String.format("Customer %d not found.", id)));
    }
    @Override
    public Long add(Customer c) {
        return this.customerRepository.save(c).getId();
    }
    @Override
    public void remove(Long id) {
        this.customerRepository.deleteById(id);
    }
}

客户控制器.java

@RestController
@RequestMapping("/customers")
public class CustomerController {
    private final ICustomerService customerService;
    public CustomerController(ICustomerService customerService) {
        this.customerService = customerService;
    }
    @GetMapping
    public List<Customer> getAll() {
        return this.customerService.getAll();
    }
    @GetMapping("/{customerid}")
    public Customer getByID(@PathVariable("customerid") Long customerid) {
        return this.customerService.getByID(customerid);
    }
    @PostMapping
    public Long add(@RequestBody Customer c) {
        return this.customerService.add(c);
    }
    @DeleteMapping("/{customerid}")
    public void remove(@PathVariable("customerid") Long customerid) {
        this.customerService.remove(customerid);
    }
}

我的POST请求示例:
curl http://localhost:8080/customers -X POST -d '{"customer_name": "John","customer_surname": "Smith"}' -H 'Content-Type: application/json'
customer表中得结果条目:

select * from customer where customerid = 1
 ------------------------------------------------
 |customerid | customer_name | customer_surname |
 -----------------------------------------------
 | 1         | NULL          | NULL             |
 ------------------------------------------------
olqngx59

olqngx591#

您的发布请求引用了表的名称,而不是Customer类中的变量。您可以修改您的请求:

{"customerName": "John","customerSurname": "Smith"}

或者修改实体,添加json属性注解,如下所示:

@Column(name = "customer_name")
@JsonProperty("customer_name")
private String customerName;
@Column(name = "customer_surname")
@JsonProperty("customer_surname")
private String customerSurname;

相关问题