-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How to use the Hibernate Validator CDI portable extension
PostPosted: Wed May 04, 2016 2:34 pm 
Newbie

Joined: Tue May 03, 2016 8:50 pm
Posts: 2
I would like to inject my original class to my customizing validator without JavaEE environment. I wrote a code for this considering the following points (#1 and #2) according to your reference guide, but I couldn’t get the instance injection (I got a NullPointerException in the isValid call).
How can I get the injection of UserServiceImpl.class, successfully?
Supposedly, ‘org.hibernate.validator.cdi.HibernateValidator’ should be used in somewhere else in the source code.
If my understanding is correct, please let me know how to use it in my codes.

#1. I added the Maven dependency to my POM as shown in Example1.3 in the reference guide.
#2. I prepared validator class and injection class as follows:

Code:
package xxxxx.constraints.impl;

import javax.inject.Inject;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import xxxxx.constraints.UnusedUserId;
import xxxxx.service.user.UserServiceImpl;

public class UnusedUserIdValidator implements ConstraintValidator<UnusedUserId, String> {
   @Inject
   UserServiceImpl userInformation;

   @Override
   public void initialize(UnusedUserId constraintAnnotation) {
   }

   @Override   
   public boolean isValid(String value, ConstraintValidatorContext context) {
      if (value == null) {
         return true;
      }
      return userInformation.isUnusedUserId(value);
   }
}


Code:
package xxxxx.service.user;

public interface UserService {
   boolean isUnusedUserId(String userId);
   // other methods
}


Code:
package xxxxx.service.user;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class UserServiceImpl implements UserService {

   @Override
   public boolean isUnusedUserId(String userId) {
// some logics
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.