Skip to main content

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

Hi,

I'm currently trying to figure out how the integration of JAX-RS, CDI and bean validation could/should work when using a MessageInterpolator (regarding ticket https://github.com/eclipse-ee4j/jaxrs-api/issues/544).
I have implemented a small example showing simple validation with a custom message interpolator: https://github.com/NiklasMehner/jaxrsValidationTest/

And for reasons unbeknownst to me this is actually kind of working in the Open Liberty Beta for JEE 8.

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).

The piece of code I'm interested in is in https://github.com/NiklasMehner/jaxrsValidationTest/blob/master/src/main/java/de/niklasmehner/HelloMessageInterpolator.java :

@javax.ws.rs.core.Context
private HttpHeaders headers;
@Inject
private Instance<HttpServletRequest> request;

This fields are actually successfully injected. From my understanding of the specs it should not actually be possible to inject HttpHeaders into a CDI-bBean. Is this correct? If yes: Is there any defined behavior in the case that the injection is not possible?
In this implementation the only way to find out if the call comes from a JAX-RS resource is the check "headers.getAcceptableLanguages() != null". Which is in my option the worst way: The object is there, the method call succeeds, but no value is returned.
(the Instance<HttpServletRequest> is broken in a similar way (request.isResolvable() returns true when the request is not available), but this is probably an implementation issue or at least covered by a different spec)

The HttpServletRequest injection also works. But I could not find anything in the spec, that indicates that the CDI request scope is actually active while a JAX-RS request is executed. Is this up to the implementors to decide?

As I have written in the comment in ticket 544 I don't think jax-rs can specify its own way of doing the bean validation, because a resource annotated with @Stateless has to follow the CDI specification. So the only way to solve this (I currently see) is to allow the injection of HttpHeaders into CDI beans (or expect that JAX-RS implementation are always based on servlets).

Is my understanding of the situation basically correct, or am I missing something here?

Thanks & best regards,
  Niklas



Back to the top