我在dto和api类中添加了正确的验证注解,但是当我通过postman输入错误数据时,它不会出错。
api类
@RestController
@RequestMapping("/laptops")
@Validated
public class LaptopApi
{
@Autowired
private LaptopService laptopService;
@Autowired
private Environment environment;
@GetMapping("/{id}")
public ResponseEntity<LaptopDTO> getLaptop(@PathVariable Integer id)
{
LaptopDTO laptop = laptopService.getLaptop(id);
return new ResponseEntity<>(laptop, HttpStatus.OK);
}
@GetMapping("/")
public ResponseEntity<List<LaptopDTO>> getLaptops()
{
List<LaptopDTO> laptops = laptopService.getLaptops();
return new ResponseEntity<>(laptops, HttpStatus.OK);
}
@PostMapping("/")
public ResponseEntity<LaptopDTO> addLaptop(@RequestBody @Valid LaptopDTO laptopDTO)
{
Integer laptopId = laptopService.addLaptop(laptopDTO);
return new ResponseEntity<>(laptopDTO, HttpStatus.OK);
}
@PutMapping("/")
public ResponseEntity<LaptopDTO> updateSpecs(@RequestBody @Valid LaptopDTO laptopDTO)
{
laptopService.updateSpecs(laptopDTO);
return new ResponseEntity<>(laptopDTO, HttpStatus.OK);
}
@DeleteMapping("{id}")
public ResponseEntity<String> deleteLaptop(@PathVariable Integer id)
{
laptopService.deleteLaptop(id);
return new ResponseEntity<>(environment.getProperty("API.LAPTOP_DELETE_SUCCESS")+" "+id,
HttpStatus.OK);
}
dto类(我在一个单元测试上运行了regex检查,它工作正常)
public class LaptopDTO
{
private Integer id;
@NotNull
private String name;
@NotNull
@Pattern(regexp = "(Intel core i[3579] [0-9]{4,}[A-Za-z]*)" +
"|(AMD Ryzen [3579] [0-9]{4,}[A-Za-z]*)",
message = "microcenter.cpu.invalid")
private String cpu;
@NotNull
@Min(value = 4)
private Integer ram;
@NotNull
private Integer nvme;
@NotNull
private Integer ssd;
@NotNull
private Integer hdd;
@NotNull
@Pattern(regexp = "(Intel U?HD [56][23]0)|" +
"([Nn]vidia Geforce [GR]TX [123][06][5678]0)(| super| Ti)",
message = "microcenter.gpu.invalid")
private String gpu;
... //constructors and methods
}
json请求正文:
{
"name": "asegjbofld;bsgj",
"cpu": "radhtrhsth",
"ram": 2,
"nvme": 512,
"ssd": 1000,
"hdd": 2000,
"gpu": "newga"
}
基于来自json请求体的无效cpu和gpu输入,它应该是错误的,因为gpu和cpu,但是请求得到了处理?有什么办法解决吗?谢谢。
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.microcenter</groupId>
<artifactId>MicroCenter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MicroCenter</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1条答案
按热度按时间bjp0bcyl1#
spring boot版本,
不支持
validator-api
,开箱即用。直到spring boot版本1.4.7,
javax.validation
有现成的支持。您需要添加依赖项:
这将增加所需的支持。