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

Anuraag Mishra
PLUS
Anuraag Mishra
Courses Plus Student 4,286 Points

Problem: When doing a Post req Via POSTMAN to http://localhost:8080/courses/ just after adding validator field.

On doing the first run in Validation video, doing the following : POST - http://localhost:8080/courses/

via POSTMAN, produced following :

{
  "timestamp": 1491542770698,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "org.springframework.beans.factory.NoUniqueBeanDefinitionException",
  "message": "No qualifying bean of type 'org.springframework.validation.Validator' available: expected single matching bean but found 2: jsr303Validator,mvcValidator",
  "path": "/api/v1/courses/"
}

and the following on the Intellij Idea

threw exception [Request processing failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.validation.Validator' available: expected single matching bean but found 2: jsr303Validator,mvcValidator] with root cause

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.validation.Validator' available: expected single matching bean but found 2: jsr303Validator,mvcValidator. 

1 Answer

Anuraag Mishra
PLUS
Anuraag Mishra
Courses Plus Student 4,286 Points

Found the Answer on Stackoverflow, This is the link link

just need to add the following in the RestConfig,

@Bean
public javax.validation.Validator localValidatorFactoryBean() {
   return new LocalValidatorFactoryBean();
}

this how my RestConfig Looks Now

package com.teamtreehouse.core;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Configuration
public class RestConfig extends RepositoryRestConfigurerAdapter {
  @Autowired
  @Lazy
  private Validator validator;

  @Override
  public void configureValidatingRepositoryEventListener(
      ValidatingRepositoryEventListener validatingListener) {
    validatingListener.addValidator("beforeCreate",validator);
    validatingListener.addValidator("beforeSave",validator);

  }

  @Bean
  public javax.validation.Validator localValidatorFactoryBean() {
    return new LocalValidatorFactoryBean();
  }
}