Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jaxrs-dev] Integrating JAX-RS, CDI and bean validation

Hi Niklas,

you are bringing up valid points. I looked a lot into the BV + JAX-RS integration as part of my work of integrating JSR 371 (MVC 1.0) with the validation mechanism provided by JAX-RS. And in my opinion the validation chapter in the JAX-RS spec needs some clarifications. It is simply not defined which ValidatorFactory/Validator should be used for the validation.

First of all: The CDI provided ValidatorFactory you mentioned is actually the "default ValidatorFactory" (as defined by BV), which is automatically configured from validation.xml. So basically CDI just provides a simple way to get this default factory. In my opinion JAX-RS implementations should use this default factory for validation. But I agree that JAX-RS implementations will need a custom MessageInterpolator in this case. However, this should be easy. Something like:

  ValidatorFactory validatorFactory = Validation.byDefaultProvider()
          .configure()
          .messageInterpolator( new CustomJaxRsMessageInterpolator() )
          .buildValidatorFactory();

This way you will get a ValidatorFactory with all the defaults from validation.xml except the MessageInterpolator, which is customized for the JAX-RS usecase. Maybe this is acceptable if it is mentioned in the spec.

However, I also agree that it would be nice to have a simpler way to provide the Locale to BV.

Christian



2018-02-28 9:01 GMT+01:00 Niklas Mehner <niklas.mehner@xxxxxxxxx>:
Hi Christian,


So what I'd like to achieve is to implement a validation message interpolator that uses the locale from the JAX-RS Headers (HelloResource.java). But since the message interpolator is global in the application it has to work for servlet requests and outside of request processing as well (TestServlet.java / SomeSingleton.java).

Why is the message interpolator global? Basically it would be possible for JAX-RS implementations or application developers to build a custom ValidatorFactory.

Well that exactly is my question: Can we actually use a different validator? Probably I haven't stated the problem I am seeing in an understandable way:

If we have a session bean:

@Stateless
public class Hello {
  public String sayHello(@NotEmpty String name) {
    return "Hello " + name;
  }
}

this bean is being validated using the global message intercepter according to the CDI specification (if I understand the spec correctly).
If we now add a Path:

@Stateless
@Path("hello")
public class Hello {
  @GET
  public String sayHello(@NotEmpty String name) {
    return "Hello " + name;
  }
}

Then we suddenly get a validation using the "jax-rs"-Validation? Does this not violate the CDI specification since Hello is still a CDI bean?

And if we decide to separate my validation from the resource:

@Path("hello")
public class HelloResource {

  @Inject
  private Hello hello;

  @GET
  public String sayHello(String name) {
    return hello.sayHello(name);
  }
}

then we suddenly get a validation messages in a different language since the CDI validation is active again?
As a developer I'd find this change in behavior quite unexpected.

Best regards,
  Niklas


_______________________________________________
jaxrs-dev mailing list
jaxrs-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jaxrs-dev




--

Back to the top