[英]Springboot: Can DTO be changed at runtime with null values not being present in the object returned from api?
[英]DTO insert null values into database in springboot
我只是想在 Spring Boot 應用程序中了解 DTO 概念所以我創建了一個客戶模式、客戶 DTO、客戶服務和控制器
但是當我通過郵遞員測試 API 時,它會為所有屬性插入空值,但我現在不知道為什么......
這就是服務:
@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 和 *** 的輸出為空,這就是我插入值的方式
任何幫助或想法?
您需要在控制器的處理程序方法 arg 上使用@RequestBody
public ResponseEntity<Customer> addCustomer(@RequestBody CustomerDTO customer) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.