英文:
Validation not working in Java Spring Application
问题
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 类(我在单元测试中运行了正则表达式检查,正常工作)
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;
... //构造函数和方法
}
JSON 请求体:
{
"name": "asegjbofld;bsgj",
"cpu": "radhtrhsth",
"ram": 2,
"nvme": 512,
"ssd": 1000,
"hdd": 2000,
"gpu": "newga"
}
基于 JSON 请求体中的无效 CPU 和 GPU 输入,应该出现错误,但请求却被处理了?对如何修复有什么想法吗?谢谢。
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>
英文:
I added the correct validation annotations to my DTO and API class, but when I input bad data thru postman, it doesn't error.
API Class
@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 Class (I ran the regex checks on a unit test, which works correctly)
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 Request Body:
{
"name": "asegjbofld;bsgj",
"cpu": "radhtrhsth",
"ram": 2,
"nvme": 512,
"ssd": 1000,
"hdd": 2000,
"gpu": "newga"
}
Based on the invalid cpu and gpu input from the json request body, it is supposed to error, because of the gpu and cpu, but the request gets processed? Any ideas on how to fix? Thanks.
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
得分: 9
The spring boot version,
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
doesn't support validator-api
,
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
out of the box. Till Spring boot version 1.4.7, javax.validation
had out-of-the-box support.
You need to add the dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
This will add the required support.
英文:
The spring boot version,
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
doesn't support validator-api
,
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
out of the box. Till Spring boot version 1.4.7, javax.validation
had out-of-the-box support.
You need to add the dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
This will add the required support.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论