Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在使用JAX-RS时,有没有一种方法可以将变量放入自定义ConstraintValidator中?_Java_Jax Rs_Bean Validation_Websphere Liberty - Fatal编程技术网

Java 在使用JAX-RS时,有没有一种方法可以将变量放入自定义ConstraintValidator中?

Java 在使用JAX-RS时,有没有一种方法可以将变量放入自定义ConstraintValidator中?,java,jax-rs,bean-validation,websphere-liberty,Java,Jax Rs,Bean Validation,Websphere Liberty,在反序列化JSON负载时,我希望使用自定义验证器验证电话号码。此验证器需要知道客户所在的国家,才能正确解析电话号码。我可以在http头接受语言中找到的国家 问题是如何向自定义验证器提供这些额外的元数据 我确信@Context会将HttpHeaders注入PhoneNumberValidator,但这不起作用: @Context private HttpHeaders mHttpHeaders; 该字段为空 API终点: @Path("customer") public clas

在反序列化JSON负载时,我希望使用自定义验证器验证电话号码。此验证器需要知道客户所在的国家,才能正确解析电话号码。我可以在http头接受语言中找到的国家

问题是如何向自定义验证器提供这些额外的元数据

我确信@Context会将HttpHeaders注入PhoneNumberValidator,但这不起作用:

    @Context
    private HttpHeaders mHttpHeaders;
该字段为空

API终点:

@Path("customer")
public class CustomerResource {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response postCustomer(@Valid Customer pCustomer) {
        return Response.ok(pCustomer).build();
    }
}
POJO:

验证程序接口:

@Documented
@Retention(RUNTIME)
@Target({ TYPE, FIELD })
@Constraint(validatedBy = { PhoneNumberValidator.class })
public @interface ValidPhoneNumber {
    String message() default "Phone number is not valid!";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}
@已记录
@保留(运行时)
@目标({TYPE,FIELD})
@约束(validatedBy={PhoneNumberValidator.class})
public@interface ValidPhoneNumber{
字符串消息()默认“电话号码无效!”;
类[]组()默认值{};

类HttpHeaders
对象还不是一个CDI管理的bean——我说它还不是,因为它是雅加达JAX-RS 3.0成为CDI bean计划的一部分,但对于JAX-RS 2.1和更早版本,它不是

但是资源类可能是CDI管理的bean。因此,您可以将
HttpHeaders
注入
CustomerResource
类,然后提供访问头的getter方法。然后在验证程序类中使用
@inject
注入资源。然后您可以从验证器

大概是这样的:

@RequestScoped
public class PhoneNumberValidator implements ConstraintValidator<ValidPhoneNumber, String> {
    @Inject
    CustomerResource customerResource;

    @Override
    public boolean isValid(String vTelephone, ConstraintValidatorContext pContext) {
        // Get the country in here using the original resource instance
        final String vCountry = customerResource.getHeaderString("COUNTRY");
        try {
            validate(vTelephone, vCountry);
        } catch (Throwable t) {
            System.out.printf("Error parsing {0} as a phone number in {1}: {2}", vTelephone, vCountry, t.getMessage());
            return false;
        }
        return true;
    }
...
@RequestScoped
@Path("customer")
public class CustomerResource {

    @Context
    private HttpHeaders headers;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response postCustomer(@Valid Customer pCustomer) {
        return Response.ok(pCustomer).build();
    }

    String getHeaderString(String headerName) {
        return headers.getHeaderString(headerName);
    }
...
我在这里写了一个工作示例,可能会有所帮助:

希望这有帮助,安迪

@RequestScoped
public class PhoneNumberValidator implements ConstraintValidator<ValidPhoneNumber, String> {
    @Inject
    CustomerResource customerResource;

    @Override
    public boolean isValid(String vTelephone, ConstraintValidatorContext pContext) {
        // Get the country in here using the original resource instance
        final String vCountry = customerResource.getHeaderString("COUNTRY");
        try {
            validate(vTelephone, vCountry);
        } catch (Throwable t) {
            System.out.printf("Error parsing {0} as a phone number in {1}: {2}", vTelephone, vCountry, t.getMessage());
            return false;
        }
        return true;
    }
...
@RequestScoped
@Path("customer")
public class CustomerResource {

    @Context
    private HttpHeaders headers;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response postCustomer(@Valid Customer pCustomer) {
        return Response.ok(pCustomer).build();
    }

    String getHeaderString(String headerName) {
        return headers.getHeaderString(headerName);
    }
...