JSR 303 Validator injection with Spring in Jersey classes

I encountered a strange behaviour that I didn't manage to explain, with a validator injection in a Jersey class with DI managed by Spring.

To sumerize the situation :

I'm using Jeysey 2 and Spring 4 to produce REST web services using the jax-rs specification, and I'm validating the bean with the jsr303 bean validation annotations. The bean validation implementation is Hibernate Validator, bundled within jersey-bean-validation dependency.

Here is my POM:

<jersey.version>2.26-b07</jersey.version>
...
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring4</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-bean-validation</artifactId>
    <version>${jersey.version}</version>
</dependency>

In my spring configuration, I declare a Validator bean (with a custom message interpolator to retrieve the error messages in database), which I'll later want to inject in my web service classes :

@Bean
public Validator getValidator(MessageInterpolator messageInterpolator){
    return Validation.byDefaultProvider().configure()
            .messageInterpolator(messageInterpolator)
            .buildValidatorFactory()
            .getValidator();
}

At this point, I'm able to inject my validator in Jersey classes using the @Autowired annotation, but not with the jsr 330 @Inject annotation which inject another validator with the default Hibernate Validator message interpolator...

@Autowired
private Validator validator; // OK

@Inject
private Validator validator; // KO

@Inject
private OtherService otherService; // OK

Can you explain me what happen here ?

I understood that the dependency injection implementation bundled with Jersey is HK2, but the jersey-spring plugin is supposed to do the bridge between HK2 and Spring, I must have missed something because they seem completely dissociated, I think that :

  • @Autowired use Spring and inject the bean I've defined
  • @Inject use HK2 which is not aware of that bean, and inject the one found in the Hibernate Validator project...
  • By the way I'm perfectly able to inject my other Spring services with the @Inject annotation, that's why I'm loosing my mind and don't know what to think... The only problematic bean seems to be this Validator.

    I also tried to use the @Valid annotation to validate my beans without having to inject the validator, and the problem is the same : Jersey use the default validator from Hibernate Validator...


    After lots of time, I found a way to fix the @Valid problem by creating a ContextResolver Provider to modify Jersey default configuration with my custom message interpolator :

    @Provider
    public class ValidationConfigContextResolver implements ContextResolver<ValidationConfig> {
    
        private final MessageInterpolator messageInterpolator;
    
        @Inject
        public ValidationConfigContextResolver(MessageInterpolator messageInterpolator) {
            this.messageInterpolator = messageInterpolator;
        }
    
        @Override
        public ValidationConfig getContext(Class<?> aClass) {
            final ValidationConfig config = new ValidationConfig();
            config.messageInterpolator(messageInterpolator);
            return config;
        }
    
    }
    

    Now Jersey use a validator with my message interpolator but I'm still trying to understand why I'm not able to inject it with the @Inject annotation in Jersey classes !

    Thanks for the assistance

    链接地址: http://www.djcxy.com/p/82030.html

    上一篇: 在jetty中初始化spring beans 7.1.4

    下一篇: JSR 303验证程序注入Spring中的Jersey类