gpt4 book ai didi

java - requestparam 的 Spring 验证不起作用

转载 作者:行者123 更新时间:2023-12-04 01:21:34 24 4
gpt4 key购买 nike

我正在尝试使用 @Min(1) 注释在 Controller 中验证我的参数。当我在单元测试中测试它时,它是 200。我不确定我做错了什么。这是我的代码:
Controller :

@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());
}
我期望它返回 400,但结果是 200。
更新:
所以我在 Controller 中添加了 @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 个步骤:

  • 将 hibernate validator 包含到 pom.xml
  • 向 Controller 添加了@Validated 注释

  • 还需要两个步骤。除了上面的,你还能做这两个吗:
  • 在 pom.xml 中再添加一项(不确定为什么需要 javax.el)


    <dependency>
    <groupId>org.glassfish </groupId>
    <artifactId>javax.el </artifactId>
    <version>3.0.1-b11 </version>
    </dependency>
  • 将以下内容添加到您的 Java 配置类


    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
    }

  • (引用 - https://www.baeldung.com/spring-validate-requestparam-pathvariable)

    关于java - requestparam 的 Spring 验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62580557/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com