I have created a spring boot application (user spring-boot-starter-parent V. 2.2.2.RELEASE) with Rest controllers, that work fine, now I have added a dependency to :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
And have added @Validated on my controller class so that all methods in it should be validated:
@RestController
@Validated
public class UserController {
@Autowired
private UserService userService;
@PostConstruct
public void init() {
System.out.println("test");
}
}
Now the controller methods when called start throwing NullPointerExceptions because the userService is null; I did a @PostConstruct as a test. Apparently it is called on a normal unenhanced bean which has the fields autowired correctly. But when calling the controller through HTTP, this unenhanced bean is not called, but it is a bean of class UserController$$EnhancerBySpringCGLIB$$ and it has the userController not autowired. I don't really know why cause this is supposed to be really simple, there is not much to configure as far as I know. So I guess for some reason spring does not inject dependencies to CGLIB enhanced classes or just injects it into a wrong class. When I remove @Validated, everything is working fine again, but there is no validation of course.
This was the most ridiculous error I had for ages. I mistakenly set controller methods in my original project as private. This caused the CGLIB to not enhance them and instead just have them with original code. Spring did not complain and is happy to run those methods