spring 请求参数验证无效
spring validation for requestparam not working
我正在尝试使用@Min(1) 注释在控制器中验证我的参数。当我在单元测试中测试它时,它是 200。我不确定我做错了什么。这是我的代码:
控制器:
@GetMapping(produces = "application/json")
public Response search(@RequestParam(name = "firstname", required = false) String firstName,
@RequestParam(name = "lastname", required = false) String lastName, @RequestParam("ibid") @Min(1) long ibId,
@RequestParam(name = "workstationids", required = false) List<Long> workstationIds,
@RequestParam("timerange") int timeRange,
@RequestParam(name = "receivingstatus", required = false) String receivingStatus,
@RequestParam(name = "displaystart", required = false) Integer displayStart,
@RequestParam(name = "displaylength", required = false) Integer displayLength,
@RequestParam("timezone") @NotBlank String timeZone) {
...
...
测试:
@Test
public void searchWithInvalidParams() throws Exception {
mockMvc.perform(get(BASE_URL)
.param(COLUMN_IB_ID, INVALID_IB_ID_VALUE) //invalid ib id is "-1"
.param(COLUMN_TIME_RANGE, TIME_RANGE_VALUE)
.param(COLUMN_TIME_ZONE, TIME_ZONE)
.andExpect(status().isBadRequest());
}
我期待它 return 400,但结果是 200。
更新:
所以我在控制器中添加了 @Validated 并添加了对 pom 的依赖,但它仍然不起作用(仍然是 200)
我的 pom:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.nuance.powershare</groupId>
<artifactId>psh-app-dispatch-reporter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>psh-app-dispatch-reporter-service</artifactId>
<properties>
<sqljdbc4.version>4.0.0</sqljdbc4.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.nuance.powershare</groupId>
<artifactId>psh-lib-dispatch-reporter-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>${sqljdbc4.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.10.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
您已正确完成以下 2 个步骤:
- 将休眠验证程序包含到 pom.xml
- 向控制器添加了@Validated 注释
还需要两个步骤。除了上面的,你还能做这两个吗:
在 pom.xml 中再添加一项(不确定为什么需要 javax.el)
<dependency>
<groupId>org.glassfish </groupId>
<artifactId>javax.el </artifactId>
<version>3.0.1-b11 </version>
</dependency>
将以下内容添加到您的 Java 配置中 class
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
(参考 - https://www.baeldung.com/spring-validate-requestparam-pathvariable)
从 Boot 2.3 开始,@RequestParam
和 @PathVariable
验证所需的步骤是:
- 添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
- 添加
@Validated
到控制器 class
我正在尝试使用@Min(1) 注释在控制器中验证我的参数。当我在单元测试中测试它时,它是 200。我不确定我做错了什么。这是我的代码:
控制器:
@GetMapping(produces = "application/json")
public Response search(@RequestParam(name = "firstname", required = false) String firstName,
@RequestParam(name = "lastname", required = false) String lastName, @RequestParam("ibid") @Min(1) long ibId,
@RequestParam(name = "workstationids", required = false) List<Long> workstationIds,
@RequestParam("timerange") int timeRange,
@RequestParam(name = "receivingstatus", required = false) String receivingStatus,
@RequestParam(name = "displaystart", required = false) Integer displayStart,
@RequestParam(name = "displaylength", required = false) Integer displayLength,
@RequestParam("timezone") @NotBlank String timeZone) {
...
...
测试:
@Test
public void searchWithInvalidParams() throws Exception {
mockMvc.perform(get(BASE_URL)
.param(COLUMN_IB_ID, INVALID_IB_ID_VALUE) //invalid ib id is "-1"
.param(COLUMN_TIME_RANGE, TIME_RANGE_VALUE)
.param(COLUMN_TIME_ZONE, TIME_ZONE)
.andExpect(status().isBadRequest());
}
我期待它 return 400,但结果是 200。
更新: 所以我在控制器中添加了 @Validated 并添加了对 pom 的依赖,但它仍然不起作用(仍然是 200)
我的 pom:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.nuance.powershare</groupId>
<artifactId>psh-app-dispatch-reporter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>psh-app-dispatch-reporter-service</artifactId>
<properties>
<sqljdbc4.version>4.0.0</sqljdbc4.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.nuance.powershare</groupId>
<artifactId>psh-lib-dispatch-reporter-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>${sqljdbc4.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.10.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
您已正确完成以下 2 个步骤:
- 将休眠验证程序包含到 pom.xml
- 向控制器添加了@Validated 注释
还需要两个步骤。除了上面的,你还能做这两个吗:
在 pom.xml 中再添加一项(不确定为什么需要 javax.el)
<dependency> <groupId>org.glassfish </groupId> <artifactId>javax.el </artifactId> <version>3.0.1-b11 </version> </dependency>
将以下内容添加到您的 Java 配置中 class
@Bean public MethodValidationPostProcessor methodValidationPostProcessor() { return new MethodValidationPostProcessor(); }
(参考 - https://www.baeldung.com/spring-validate-requestparam-pathvariable)
从 Boot 2.3 开始,@RequestParam
和 @PathVariable
验证所需的步骤是:
- 添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
- 添加
@Validated
到控制器 class