Spring @Valid 注释不能用作验证 dto 字段?
Spring @Valid annotation not working as validating dto fields?
我已经检查了很多关于它的其他问题,但我找不到解决方案,我在哪里遗漏了?
控制器方法如下:
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 field 将以 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>
这里的问题是您将 id
定义为原始类型,它永远不能为 null。
如果您选择盒装类型(Integer
在这种情况下),这应该有效。
所以在你的 DTO 中:
import javax.validation.constraints.NotNull;
@Data
public class CustomerUpdateRequestDto {
@NotNull
private Integer id;
...
}
我已经检查了很多关于它的其他问题,但我找不到解决方案,我在哪里遗漏了?
控制器方法如下:
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 field 将以 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>
这里的问题是您将 id
定义为原始类型,它永远不能为 null。
如果您选择盒装类型(Integer
在这种情况下),这应该有效。
所以在你的 DTO 中:
import javax.validation.constraints.NotNull;
@Data
public class CustomerUpdateRequestDto {
@NotNull
private Integer id;
...
}