@NotNull 注释未检查 Jersey REST 资源中的空查询参数

2024-05-04

我正在尝试使用 javax.validation.validation-api 进行验证@QueryParam参数。我已按照以下步骤操作:

  1. 添加依赖项:

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-bean-validation</artifactId>
        <version>2.12</version>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
  2. 泽西岛资源:

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{param1}")
    @ValidateOnExecution
    public SomeObject get(@PathParam("param1") String param1,
        @NotNull @QueryParam("q1") String q1,
        @NotNull @QueryParam("q2") String q2) {
    
        try {
            //Assuming q1 and q2 are NOT Null here
            ...
        } catch(Exception exception) { 
            throw new WebApplicationException(exception, 
                    Response.Status.INTERNAL_SERVER_ERROR);
        }
        return someObject;
    }     
    
  3. 我尝试给出各种 URL 组合,如 q1 和 q2 中两个参数均不存在,q1 不存在或 q2 不存在。每一次,@NotNull没有被检测到。意味着无论 q1 和 q2 是否存在,try 块代码都会被执行null.

还需要做什么?

我提到了这个链接 -https://jersey.java.net/documentation/latest/bean-validation.html#d0e11956 https://jersey.java.net/documentation/latest/bean-validation.html#d0e11956.

如何检查我的环境中是否启用了自动发现功能?


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

@NotNull 注释未检查 Jersey REST 资源中的空查询参数 的相关文章

随机推荐