Skip to content

@Validated don't work on interface #17000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
longscoop opened this issue May 29, 2019 · 4 comments
Closed

@Validated don't work on interface #17000

longscoop opened this issue May 29, 2019 · 4 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@longscoop
Copy link

longscoop commented May 29, 2019

Springcloud is used in the project, but returns 404

@Validated
public interface ValidClient {

	@GetMapping("/valid")
	String queryById(@RequestParam("id") @Min(1) @Max(400) Integer id);

}
@RestController
public class ValidController implements ValidClient {

	@Override
	public String queryById(@Min(1) @Max(400) Integer id) {
		return "id:" + id;
	}
}

Springcloud is used in the project, but returns 404,the Validated don't work on interface ?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 29, 2019
@wilkinsona
Copy link
Member

For the record, @Validated should work on interfaces, however, from the information provided, I don't think this has anything to do with @Validated. If you are getting a 404 response, your controller method hasn't been called so the point where validation would have been performed will not have been reached.

Since Spring Framework 5.1, request mappings should be found on interfaces due to the changes made in spring-projects/spring-framework#15682. Perhaps you are using a version of Spring Boot that uses Spring Framework 5.0 or earlier?

If you'd like us to spend any more time on this, you'll need to spend some time providing some more information about your problem. Please create a minimal sample that reproduces your problem and share it in a separate GitHub repository or in a zip attached to this issue.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label May 29, 2019
@longscoop
Copy link
Author

hi @wilkinsona ,I add GitHub repository use spring boot 2.1

@longscoop
Copy link
Author

this is GitHub repository https://github.com/longscoop/spring-boot-validated

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 31, 2019
@wilkinsona
Copy link
Member

Thanks for the sample.

The problem is caused by your validation configuration, specifically the MethodValidationPostProcessor. @Validated beans are proxied so that calls to their methods can be intercepted and validation performed. The configuration of MethodValidationPostProcessor determines the type of proxy that is created. Spring Boot's default configuration is to use CGLib proxies all the time. Your configuration results in a JDK-based proxy being used if the validated class implements a single interface. That is the case with your ValidController class. A limitation of JDK-based proxies is that annotation information is lost. As a result your ValidController is not identified as a @RestController so its @RequestMapping is ignored and you get a 404 when trying to call it.

You can fix the problem by removing your @Bean method for MethodValidationPostProcessor and using the auto-configured post-processor instead. Alternatively, you can call setProxyTargetClass(true) on your post-processor definition so that it always creates CGLib-based proxies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants