相关疑难解决方法(0)

我可以在ConstraintValidator中为Method参数更改属性路径吗?

如果您熟悉Bean验证框架,则您知道无法获取方法参数的名称.因此,如果对方法的第一个参数执行@NotNull约束并且验证失败,则getPropertyPath将类似于"arg1".

我想创建我自己的@NotNull版本,它可以取一个值,例如@NamedNotNull("emailAddress").但我无法弄清楚如何覆盖我的Validator中的#getPropertyPath?有没有办法做到这一点,或者我坚持使用"arg1"或"arg2"等.

编辑

根据我收到的答案,我能够提出以下实现,允许我从@QueryParam或@PathParam注释中获取值,并将其用作Bean验证注释(如@NotNull)的属性路径.

对于Jersey,您需要创建以下类.请注意DefaultParameterNameProvider的实现:

public class ValidationConfigurationContextResolver implements ContextResolver<ValidationConfig> {
    @Override
    public ValidationConfig getContext( final Class<?> type ) {
        final ValidationConfig config = new ValidationConfig();
        config.parameterNameProvider( new RestAnnotationParameterNameProvider() );
        return config;
    }

    static class RestAnnotationParameterNameProvider extends DefaultParameterNameProvider {

        @Override
        public List<String> getParameterNames( Method method ) {
            Annotation[][] annotationsByParam = method.getParameterAnnotations();
            List<String> names = new ArrayList<>( annotationsByParam.length );
            for ( Annotation[] annotations : annotationsByParam ) {
                String name = getParamName( annotations );
                if ( name == null )
                    name = …
Run Code Online (Sandbox Code Playgroud)

java hibernate bean-validation jersey-2.0

23
推荐指数
2
解决办法
7568
查看次数

Jersey Client/JAX-RS和可选(非默认)@QueryParam(客户端)

我有一个RESTful API,他的文档说某个查询参数是可选的,并且不提供默认参数.因此,我可以提供值,也可以不在GET请求中将其作为参数发送.

例:

  • queryA 是必须的
  • queryB可选的(GET没有它可以发送)

这应该工作:

http://www.example.com/service/endpoint?queryA=foo&queryB=bar
Run Code Online (Sandbox Code Playgroud)

这应该也有效:

http://www.example.com/service/endpoint?queryA=foo
Run Code Online (Sandbox Code Playgroud)

如何为Jersey-Proxy创建一个可以执行此操作的客户端界面?我没有与服务器端代码进行交互,因此我使用org.glassfish.jersey.client.proxy.WebResourceFactoryJersey-Proxy来生成客户端以与服务器API进行交互.

样本界面:

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/service")
@Produces("application/json")
public interface ServiceInterface {

    @Path("/endpoint")
    @GET
    public Response getEndpoint(
            @QueryParam("queryA") String first,
            @QueryParam("queryB") String second);

}
Run Code Online (Sandbox Code Playgroud)

我知道我可以制作另一种方法:

    @Path("/endpoint")
    @GET
    public Response getEndpoint(
            @QueryParam("queryA") String first);
Run Code Online (Sandbox Code Playgroud)

但是当你有多个可选字段时会发生什么?我不想让它们发生任何可能的变异!

java rest jax-rs jersey

17
推荐指数
1
解决办法
3万
查看次数

@NotNull注释不检查Jersey REST资源中的null queryparameter

我正在尝试使用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>
    
    Run Code Online (Sandbox Code Playgroud)
  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;
    }     
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我尝试给出各种URL的组合,如在q1和q2中,两个参数都不存在,q1缺席或q2缺席.每次@NotNull都没有被发现.意味着尝试块代码正在执行,而不管q1和q2是什么null.

还有什么需要做的?

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

如何在我的环境中检查Auto-Discoverable功能是否已启用?

rest jersey bean-validation

10
推荐指数
0
解决办法
5023
查看次数

将JAX-RS bean验证错误消息绑定到视图

我们可以使用bean验证轻松验证JAX-RS资源类字段或方法参数,如下所示:

@Size(min = 18,max = 80,message ="Age必须介于{min}和{max}之间.")字符串年龄;

将错误消息绑定到JSP页面的最简单方法是什么?

(比方说,我正在使用带有Jersey或Resteasy的Java EE 7)

jax-rs jersey java-ee resteasy bean-validation

7
推荐指数
1
解决办法
7907
查看次数