如何使用 @RequestParam 进行验证

发布于 2025-01-12 06:42:43 字数 714 浏览 2 评论 0原文

给定 Spring Boot 2.6.3Hibernate validator 6.2.0.Final,运行以下代码后:

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.Min;

@RestController
@Validated
public class TestController {
    @GetMapping("/test")
    public Integer test( @Min(10) @RequestParam Integer val) {
        return val;
    }
}

如果我调用 http://localhost:8080/test?val= 0 ,它返回 0 并且似乎忽略了 @Min(10) 部分。

有谁知道是否可以验证 @RequestParam 参数?

Given Spring Boot 2.6.3, Hibernate validator 6.2.0.Final, after running the code below:

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.Min;

@RestController
@Validated
public class TestController {
    @GetMapping("/test")
    public Integer test( @Min(10) @RequestParam Integer val) {
        return val;
    }
}

If I call http://localhost:8080/test?val=0 , it returns 0 and it seems it ignores @Min(10) part.

Does anybody know whether it is possible to validate @RequestParam parameters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你げ笑在眉眼 2025-01-19 06:42:43

我从 https://start.spring.io/ 生成了一个 Spring 项目,如下所示:

在此处输入图像描述

然后我将控制器代码添加到您的问题,以及如下集成测试:

class DemoApplicationTests {
    @Autowired
    private MockMvc mockMvc;

    @Test
    void testGoodInput() throws Exception {
        mockMvc
                .perform(MockMvcRequestBuilders.get("/test?val=10"))
                .andExpect(MockMvcResultMatchers.status().isOk());
    }

    @Test
    void testBadInput() {
        Throwable ex = catchThrowable(() -> mockMvc
                .perform(MockMvcRequestBuilders.get("/test?val=0"))
                .andReturn());
        var cause = getViolationExceptionFromCause(ex);
        assertThat(cause)
                .isInstanceOf(ConstraintViolationException.class);
    }

    private ConstraintViolationException getViolationExceptionFromCause(Throwable ex) {
        if (ex == null || ex instanceof ConstraintViolationException) {
            return (ConstraintViolationException) ex;
        }
        return getViolationExceptionFromCause(ex.getCause());
    }
}

这按预期工作,val=0 抛出 ConstraintViolationException。现在轮到你证明事实并非如此了。

I generated a Spring project from https://start.spring.io/ as shown below:

enter image description here

I then added the controller code in your question, and an integration test as below:

class DemoApplicationTests {
    @Autowired
    private MockMvc mockMvc;

    @Test
    void testGoodInput() throws Exception {
        mockMvc
                .perform(MockMvcRequestBuilders.get("/test?val=10"))
                .andExpect(MockMvcResultMatchers.status().isOk());
    }

    @Test
    void testBadInput() {
        Throwable ex = catchThrowable(() -> mockMvc
                .perform(MockMvcRequestBuilders.get("/test?val=0"))
                .andReturn());
        var cause = getViolationExceptionFromCause(ex);
        assertThat(cause)
                .isInstanceOf(ConstraintViolationException.class);
    }

    private ConstraintViolationException getViolationExceptionFromCause(Throwable ex) {
        if (ex == null || ex instanceof ConstraintViolationException) {
            return (ConstraintViolationException) ex;
        }
        return getViolationExceptionFromCause(ex.getCause());
    }
}

This works as expected, val=0 throws a ConstraintViolationException. It's your turn to prove otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文