Framework Error
The Bean validation provider Spring Boot is unable to inject the @Valid annotation into your entity due to a missing @Configuration class that is annotated with @EnableAutoConfiguration
What This Error Means
This error occurs when Spring Boot's auto-configuration feature is not properly enabled in the application, typically due to a missing or incorrectly configured configuration class.
Why It Happens
This error happens because Spring Boot's auto-configuration relies on the presence of a configuration class annotated with @EnableAutoConfiguration. If this class is missing or incorrectly configured, the @Valid annotation used for bean validation will not be properly injected into the entity, resulting in this error.
How to Fix It
- 1To fix this error, add a configuration class annotated with @EnableAutoConfiguration to your application. For example, if you're using the Spring Boot Starter Web module, you can add the following configuration class:
- 2// Before (broken code)
- 3// After (fixed code)
- 4@SpringBootApplication
- 5class MyApplication {
- 6 public static void main(String[] args) {
- 7 SpringApplication.run(MyApplication.class, args);
- 8 }
- 9}
- 10// Then, add the @Valid annotation to your entity class as follows:
- 11@Entity
- 12public class User {
- 13 @Id
- 14 @GeneratedValue(strategy = GenerationType.IDENTITY)
- 15 private Long id;
- 16 @Size(min = 5, max = 50)
- 17 @Column(name = 'username', unique = true)
- 18 private String username;
- 19 @Valid
- 20 private Address address;
- 21}
- 22class Address {
- 23 private String street;
- 24 private String city;
- 25}
Example Code Solution
@SpringBootApplication
class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Size(min = 5, max = 50)
@Column(name = 'username', unique = true)
private String username;
@Valid
private Address address;
}
class Address {
private String street;
private String city;
}@SpringBootApplication
class MyApplication {
@EnableAutoConfiguration
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Size(min = 5, max = 50)
@Column(name = 'username', unique = true)
private String username;
@Valid
private Address address;
}
class Address {
private String street;
private String city;
}Fix for The Bean validation provider Spring Boot is unable to inject the @Valid annotation into your entity due to a missing @Configuration class that is annotated with @EnableAutoConfiguration
Browse Related Clusters
Related JAVA Errors
Related JAVA Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error