我已經檢查了許多其他有關它的問題,但我找不到解決方案,我錯過了哪里?
這是控制器方法:
package com.nishberkay.nishcustomer.controller;
import com.nishberkay.nishcustomer.dto.request.CustomerAddRequestDto;
import com.nishberkay.nishcustomer.dto.request.CustomerUpdateRequestDto;
import com.nishberkay.nishcustomer.dto.response.CustomerDto;
import com.nishberkay.nishcustomer.entity.mysqlentity.Customer;
import com.nishberkay.nishcustomer.exception.InvalidRequestException;
import com.nishberkay.nishcustomer.service.CustomerService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@RestController
@RequestMapping("/customer")
@Validated
public class CustomerController {
private CustomerService customerService;
private ModelMapper modelMapper;
@Autowired
public CustomerController(CustomerService customerService, ModelMapper modelMapper) {
this.customerService = customerService;
this.modelMapper = modelMapper;
}
@PutMapping
public CustomerDto updateCustomer(@Valid @RequestBody CustomerUpdateRequestDto customerDto,
BindingResult bindingResult) throws Exception {
if (bindingResult.hasErrors()) {
String errorMessage = bindingResult.getAllErrors().get(0).getDefaultMessage();
throw new InvalidRequestException(errorMessage);
}
Customer customer = modelMapper.map(customerDto, Customer.class);
return modelMapper.map(customerService.update(customer), CustomerDto.class);
}
}
我的 dto 是:
import javax.validation.constraints.NotNull;
@Data
public class CustomerUpdateRequestDto {
@NotNull
private int id;
@NotNull
private String firstName;
@NotNull
private String lastName;
}
我的問題是(也許@Valid 是否真的有效,我不確定),當我使用郵遞員請求除錯它時:
{
"firstName" : "berkayaaasd",
"lastName" : "dada"
}
我期待某種訊息,例如“Id 不能為空”,但id 欄位將顯示為 0,所以這就是為什么我認為@Valid可能正在作業但還有其他問題。
這是 pom 依賴項:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.8</version>
</dependency>
</dependencies>
uj5u.com熱心網友回復:
這里的問題是您將您定義id為原始型別,它永遠不能為空。
如果您選擇盒裝型別(Integer在這種情況下),這應該可以。
所以在你的 DTO 中:
import javax.validation.constraints.NotNull;
@Data
public class CustomerUpdateRequestDto {
@NotNull
private Integer id;
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/462065.html
