JSR 303 Validator injection with Spring in Jersey classes - spring

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

Related

NoClassDeffFoundError: org/springframework/data/jpa/repository/Repository

I am using JpaRepository, here is my code
public Interface EmpRepository extends JpaRepository<Employee, Integer> {}
class EmployeeServicImpl {
private EmpRepository empRepository;
#Autowired
EmployeeServicImpl (EmpRepository theRepository) {
this.empRepository = theRepository;
}
}
added below dependencies in my pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
While start the application, resolution of declared constructor of bean from class loader : NoClassDeffFoundError : /org/springframework/data/jpa/repository/Repository
Some points,
Keep your repository interface in a separate package.
Use #Repository on your repository.
Use #Service or #Component annotation at EmployeeServicImpl class.
It appears as when you're starting the application, Spring is trying to find EmpRepository dependency to instantiate your service but is not able to find the repository since it is not declared as a #Repository by you.
Further, reason for using #Service is so that EmployeeServicImpl becomes available to Spring too.

Spring RequestParam validation in Kotlin is not working

I am trying to validate #RequestParam in Kotlin, however it does not work. Currently I am using Kotlin 1.4.20, Spring boot 2.3.5, and java 1.8.
Here is my controller:
#Validated
#RestController
#RequestMapping("api/v1/")
class myController{
#GetMapping("/age", produces = [MediaType.APPLICATION_JSON_VALUE])
fun findArticlesByAge(#RequestParam #Valid #Min(6) age: Int): ResponseEntity<Article> =
ResponseEntity
.ok()
.body(Article())
}
Hibernate validator is already in the effective pom:
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.6.Final</version>
</dependency>
Request:
http://localhost:8080/api/v1/age?age=2
Response:
200
That is the simple validation which is not working, however I want to do more complex validations through custom annotations and ConstraintValidator. If I make it work with the simple case #Min(6) probably it will start working also with the custom annotation.
So there was a dependency missing:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
I added this and the validation started working. However, it throws 500 internal server error with the validation message, which can be fixed by a ControllerAdvice.

Error creating bean with name 'gemfireCache': FactoryBean threw exception on object creation

I am trying to create an "employee" Region and put some data into it. But, I am getting Exception below:
[warn 2018/12/27 17:15:46.518 IST tid=0x1] Exception
encountered during context initialization - cancelling refresh
attempt: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'gemfireConfiguration': Injection of
resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'gemfireCache': FactoryBean threw exception on
object creation; nested exception is java.lang.NoClassDefFoundError:
it/unimi/dsi/fastutil/ints/Int2ObjectOpenHashMap
[warn 2018/12/27 17:15:46.519 IST tid=0x1] Invocation of
destroy method failed on bean with name 'gemfireCache':
org.apache.geode.cache.CacheClosedException: A cache has not yet been
created.
[error 2018/12/27 17:15:46.522 IST tid=0x1] Caught exception
while allowing TestExecutionListener
[org.springframework.test.context.web.ServletTestExecutionListener#c667f46]
to prepare test instance
[com.gemfire.demo.Gemfire1ApplicationTests#48bfb884]
Domain class
#Region("employee")
public class Employee {
#Id
public String name;
public double salary;
...
}
Repository class
#Repository
public interface EmployeeRepository extends CrudRepository<Employee, String> {
Employee findByName(String name);
}
Configuration class
#Configuration
#ComponentScan
#EnableGemfireRepositories(basePackages = "com.gemfire.demo")
public class GemfireConfiguration {
#Autowired
EmployeeRepository employeeRepository;
#Bean
Properties gemfireProperties() {
Properties gemfireProperties = new Properties();
gemfireProperties.setProperty("name", "SpringDataGemFireApplication");
gemfireProperties.setProperty("mcast-port", "0");
gemfireProperties.setProperty("log-level", "config");
return gemfireProperties;
}
#Bean
#Autowired
CacheFactoryBean gemfireCache() {
CacheFactoryBean gemfireCache = new CacheFactoryBean();
gemfireCache.setClose(true);
gemfireCache.setProperties(gemfireProperties());
return gemfireCache;
}
#Bean(name="employee")
#Autowired
LocalRegionFactoryBean<String, Employee> getEmployee(final GemFireCache cache) {
LocalRegionFactoryBean<String, Employee> employeeRegion = new LocalRegionFactoryBean<String, Employee>();
employeeRegion.setCache(cache);
employeeRegion.setClose(false);
employeeRegion.setName("employee");
employeeRegion.setPersistent(false);
employeeRegion.setDataPolicy(DataPolicy.PRELOADED);
return employeeRegion;
}
}
POM.XML
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-gemfire</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.0</version>
</dependency>
Adding additional tips with your above GemFire/Spring JavaConfig configuration class above.
Given you are using Spring Data Kay (implied by your use of the Spring Boot 2.0.x parent POM, i.e. org.springframework.boot:spring-boot-dependencies; see here), then you could be using Spring Data GemFire's (relatively) new and convenient Annotation-based configuration model.
By doing so, your GemfireConfiguration class above would become...
#PeerCacheApplication
#EnableGemfireRepositories(basePackages = "com.gemfire.demo")
class GemfireConfiguration {
#Bean(name="employee")
LocalRegionFactoryBean<String, Employee> getEmployee(GemFireCache cache) {
LocalRegionFactoryBean<String, Employee> employeeRegion =
new LocalRegionFactoryBean<String, Employee>();
employeeRegion.setCache(cache);
employeeRegion.setClose(false);
employeeRegion.setDataPolicy(DataPolicy.PRELOADED);
return employeeRegion;
}
}
A few things to keep in mind:
#PeerCacheApplication is meta-annotated with #Configuration so you do not need the explicit Spring #Configuration annotation on the configuration class.
#PeerCacheApplication allows you to adjust the GemFire log-level (along with other logging configuration) using the logLevel annotation attribute. Similarly, you can set the log-level using the corresponding property, spring.data.gemfire.cache.log-level in a Spring Boot application.properties file (see here). There are many other attributes and corresponding properties (e.g. name) you can use to adjust and customize other configuration.
While String-based package names are supported on #EnableGemfireRepositories and similar annotations, we generally prefer and recommend users to use the type-safe variant basePacakgeClasses. You only need to refer to a single type from each top-level package where your application Repositories are kept.
The explicit #Autowired annotation is not needed on your bean definitions. You do not need to explicit inject the EmployeeRepository in the configuration class to have it initialized; just inject it into the #Service class where it will be used.
For convenience, the name ("employee") of the Region bean definition on your LOCAL "employee" Region, will also be used as the name of the Region, so employeeRegion.setName("employee") is unnecessary.
You should not combine LocalRegionFactoryBean.setPersistent(:boolean) with LocalRegionFactoryBean.setDataPolicy(:DataPolicy) since the DataPolicy is going to take precedence.
While #ComponentScan is perfectly acceptable and even convenient in development, I generally do not prefer nor recommend users to use component-scanning. It is usually always better to be explicit.
As stated in the comments, you chould remove <relativePath/> from your parent definition in your application Maven POM file.
Final note, as of this post, Spring Boot 2.0.8.RELEASE is the latest release.
As for your classpath issues, if you are using Maven correctly, then Maven should take care of pulling in the correct transitive dependencies.
You can refer to the many examples I have in this repo for further clarification.
Hope this helps!
As mentioned in comments, the error shows some dependencies (java.lang.NoClassDefFoundError: it/unimi/dsi/fastutil/ints/Int2ObjectOpenHashMap) are missing. Please add corresponding dependencies in your pom.xml

How to autowire Hibernate SessionFactory in Spring boot

I've created a spring boot application, and I want to handle the Hibernate SessionFactory, so in my service class, I can just call the Hibernate SessionFactory as following :
#Autowired
private SessionFactory sessionFactory;
I found a similar question in stackoverflow where I have to add the following line in application.properties :
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
but I'm getting this error :
Cannot resolve property 'current_session_context_class' in java.lang.String
How can I solve this ?
pom.xml dependencies :
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
Since version 2.0, JPA provides easy access to the APIs of the underlying implementations. The EntityManager and the EntityManagerFactory provide an unwrap method which returns the corresponding classes of the JPA implementation.
In the case of Hibernate, these are the Session and the SessionFactory.
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
Try enabling HibernateJpaSessionFactoryBean in your Spring configuration.
#Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
return new HibernateJpaSessionFactoryBean();
}
Have a look at:
https://stackoverflow.com/a/33881946/676731
By Spring configuration I mean a class annotated with #Configuration annotation or #SpringBootApplication (it is implicitly annotated with #Configuration).

Hibernate Validator 5.1.1 is not working in Web application

I am using Spring 4.0 and latest Spring Security in my Web application. I want to use the Hibernate validation 5.1.1 Final. But It's not working. The same code works in my JUNIT but not in the web application.
The code which works:
#Test
public void testUserAuthenticationRequestValidation(){
try{
UserAuthenticationRequest req=new UserAuthenticationRequest().addUserName("bla").addPasswd("passw");
Validator validation=Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<UserAuthenticationRequest>> constraintViolations= validation.validate(req);
Assert.assertTrue("There are some errors", constraintViolations.size()>0);
System.out.println(constraintViolations);
}catch(Exception ex){
ex.printStackTrace();
Assert.fail();
}
But if I try to use the same code in my web application, it doesn't work, In the below code, I am expecting contraintViolation set not to be empty because I intentionally left both the username & password empty but I always find it empty.
#Override
public UserDetails retrieveUser(String username,
UsernamePasswordAuthenticationToken authentication)
throws AuthenticationException {
LOGGER.debug("About to check authentication for username: {}", username);
final UserAuthenticationRequest userData = new UserAuthenticationRequest()
.addUserName(username).addPasswd(
authentication.getCredentials().toString());
Validator validation = Validation.buildDefaultValidatorFactory()
.getValidator();
Set<ConstraintViolation<UserAuthenticationRequest>> constraintViolations = validation
.validate(userData);
LOGGER.info("Constraints Violations: {}", constraintViolations);
LOGGER.info("Obejct: username: {} password: {}",
userData.getUsername(), userData.getPasswd());
UserDetailVO userDetailVO = new UserDetailVO(
userInfoService.authenticate(userData));
return userDetailVO;
}
So far I have also tried in vain initializing the validation bean as shown below and injecting in my Class as well as shown below:
#Configuration
#ComponentScan({ "com.sell.mystuff.web.security",
"com.sell.mystuff.web.service" })
#ImportResource({ "classpath*:spring/common-context.xml" })
public class CommonAppContext {
#Bean(name = "javaxValidator")
public Validator getValidator() {
return Validation.buildDefaultValidatorFactory().getValidator();
}
}
I have even tried to explicitly initialized in the validation bean as shown above but nothing seems to be working.
Below is my partial POM.xml file related to validation api:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>5.1.1.Final</version>
</dependency>
Do you have the validator bean configured?
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
This will bootstrap Bean Validation 1.0 or 1.1, depending on which version of Hibernate Validator you have on the classpath. See also http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html
Even if you plan to manually call the validation, you should do it by retrieving the configured factory bean, since this will guarantee proper caching of the factory.
I got this working by adding a later version of javax.el
e.g.
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
BTW I didn't need to define a LocalValidatorFactoryBean in my spring mvc config.

Resources