DTO插入null值在Springboot中的数据库中

发布于 2025-02-09 21:07:30 字数 1099 浏览 2 评论 0原文

我只是想在Spring Boot应用程序中浏览DTO概念,因此我创建了客户模式,客户DTO,客户服务和控制器,

但是当我通过Postman测试API时,它是所有vertivbutes的插入null值,但我不知道现在为什么...

这是服务

@Override
@Transactional
public Customer addCustomer(Customer customer) {
    Customer newCustomer = cutomerRepository.save(customer);
    return newCustomer;
}

最后,这是控制器

public ResponseEntity<Customer> addCustomer(CustomerDTO customer) {
    try {
        System.out.println(customer.getEmail()+"000000000000000000000000");
        Customer newCustomer = customerService
                .addCustomer(new Customer(customer.getSerialNumber(), customer.getFirstName(),customer.getLastName(),customer.getEmail(),customer.getMobileNumber()));
        System.out.println(customer.getEmail()+ "************************");
        return new ResponseEntity<>(newCustomer, HttpStatus.CREATED);
    } catch (Exception e) {
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

000和***的输出是无效的,这就是我插入的方式重视

任何帮助或想法?

I'm just trying to walk through DTO concept in spring boot app So I create a customer mode, Customer DTO, Customer Service and Controller

But when i test the API through postman, it's insert null values for all attrivbutes, but i don't now why...

and this is the service :

@Override
@Transactional
public Customer addCustomer(Customer customer) {
    Customer newCustomer = cutomerRepository.save(customer);
    return newCustomer;
}

and at last this is the controller :

public ResponseEntity<Customer> addCustomer(CustomerDTO customer) {
    try {
        System.out.println(customer.getEmail()+"000000000000000000000000");
        Customer newCustomer = customerService
                .addCustomer(new Customer(customer.getSerialNumber(), customer.getFirstName(),customer.getLastName(),customer.getEmail(),customer.getMobileNumber()));
        System.out.println(customer.getEmail()+ "************************");
        return new ResponseEntity<>(newCustomer, HttpStatus.CREATED);
    } catch (Exception e) {
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

the output from 000 and *** are null, and this is how i insert values

Any help or Idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2025-02-16 21:07:30

您需要@requestbody在控制器的处理程序方法上

public ResponseEntity<Customer> addCustomer(@RequestBody CustomerDTO customer) {

You need @RequestBody on controller's handler method arg

public ResponseEntity<Customer> addCustomer(@RequestBody CustomerDTO customer) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文