Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJT Keller
12,731 PointsSpring Rest API - Could Not Autowire. No beans of 'Validator' type found.
I'm receiving the following error: "Could Not Autowire. No beans of 'Validator' type found." when autowiring the Validation validator. I've suppressed the warning because this is a false positive, but I'm wondering why Craig didn't run into the issue in this video?
Based on: http://stackoverflow.com/questions/26889970/intellij-incorrectly-saying-no-beans-of-type-found-for-autowired-repository. This appears to be an issue when using the @SpringApplication annotation vs. @Configuration, @EnableAutoConfiguration and @ComponentScan. Any thoughts?
6 Answers
Tommy May
12,056 PointsI ran into this issue as well. The Spring framework stopped providing it's own validator, instead they just provide an interface for a validator
The fix is to use another validator, in my case I chose Hibernate. If you want to follow along add this line to your dependencies in your build.gradle file
compile 'org.hibernate:hibernate-validator'
Delete the RestConfig class mentioned in this video as it is no longer needed, and bam the @NotNull annotation should be working now, give it a try in postman.
Craig Dennis
Treehouse TeacherHi JT Keller !
Looks like there is a problem in the later versions of Spring Boot that I didn't encounter when I originally recorded it. To workaround the current issue simply add the @Lazy
annotation to the validator declaration.
There is a circular reference which this helps avoid, and since we don't need things immediately at startup, this lets us enable it when we need it.
Hope that helps, and sorry for the delay!
Albert Zenko
6,548 PointsI've added the @Lazy annotation and it produced NullPointerException when Postman tried to create new Course
Kevin Perez
Courses Plus Student 8,180 PointsI don't know how good is do this but I fixed it putting this line of code insted of "@Autowired private Validator validator"
@Bean public Validator validator() {
return new LocalValidatorFactoryBean();
}
@Override public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
validatingListener.addValidator("beforeCreate", validator());
validatingListener.addValidator("beforeSave", validator());
}
maivandahmadzai
3,467 PointsSame problem here. No solution yet!
Craig Dennis
Treehouse TeacherDid you try that @Lazy
annotation? The
maivandahmadzai
3,467 PointsYes, I tried but it didn't help.
maivandahmadzai
3,467 PointsI got some help from stackoverflow and wanted to share with you in case someone else will need it.
Kyle Hebert
13,391 PointsKyle Hebert
13,391 PointsA lot of times when you get a warning like that and the instructors don't is because of differences between the Community Edition and Ultimate Edition. The courses here are taught with Community Edition. Were you by chance using the Ultimate edition?
It's also possible they saw the error when preparing the course, and then suppressed it before recording the video.