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 trial

Java Validation

JT Keller
JT Keller
12,731 Points

Spring 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?

Kyle Hebert
Kyle Hebert
13,391 Points

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

6 Answers

Tommy May
Tommy May
12,056 Points

I 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
STAFF
Craig Dennis
Treehouse Teacher

Hi 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
Albert Zenko
6,548 Points

I've added the @Lazy annotation and it produced NullPointerException when Postman tried to create new Course

Kevin Perez
PLUS
Kevin Perez
Courses Plus Student 8,180 Points

I 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());

}

source: https://codedump.io/share/rFr3UkwaNTS8/1/spring-validator-could-not-autowire-no-beans-of-39validator39-type-found

Same problem here. No solution yet!

Craig Dennis
Craig Dennis
Treehouse Teacher

Did you try that @Lazy annotation? The

Yes, I tried but it didn't help.

I got some help from stackoverflow and wanted to share with you in case someone else will need it.

stackoverflow