#Valid working without spring-mvc? - spring

I'd like to use #Valid annotation to validate my beans in controller method. Unfortunatelly it does not work. I know that in order to make it work I'd have to include spring-mvc into my project and put there mvc:annotation-driven or #EnableMvc... .
But I do not use spring-mvc! I use Wicket framework. How to make #Valid working without incorporating spring-mvc?
Thanks!

#Valid is not specific to spring it is an implementation of JSR 303 bean validation. You can use any other reference implementation or write your own. e.g Apache and Hibernate Validator has reference implementation available. Take a look at this answer Is there an implementation of JSR-303 (bean validation) available?

Related

Spring Boot Best approach for validation in facade

I use Spring Boot with Kotlin and my project is splitted on modules where I have application layer with REST controllers and facades, domain layer with services and business logic and infrastructures layer for communication with external services or DBs. I think (but I can be wrong) that the best place for basic validation like notNull, notEmpty, max and min, size etc. is facade because both REST controllers and another modules communicate with it, but the problem is that javax validation annotation like #Valid are working only on REST Controller layer (in classes with annotation #RestController) and when I try to create some tests for this facade then fields with wrong values return NullPointerException instead of MethodArgumentNotValidException. I tried to create WebMvcTest but also returns wrong exception. Is it some solution for it? I saw that I could call some validator inside the method but it looks like much more complex approach than annotation on method's argument.
You can inject the javax.validation.Validator into any Spring component. For example, into a facade component, and perform validation in the facade layer:
#Autowired
Validator validator;
Set<ConstraintViolation<UserRequestDTO>> violations = validator.validate(userRequest);
This way you can remove the #Valid annotation from controllers at all. And this approach puts validation results under the your control.

Spring TypeFilter for method annotations to be used in ClassPathScanningCandidateComponentProvider#findCandidateComponents

Is there an existing Spring org.springframework.core.type.filter.TypeFilter implementation for Method annotations?
I am looking to call ClassPathScanningCandidateComponentProvider#findCandidateComponents in order got get all of the components that have an annotation or inherited annotations so I can validate the state of the configuration supports the annotation attribute values.
There is an org.springframework.core.type.filter.AnnotationTypeFilter, is there a similar implementation but getting classes that have methods that are annotated with the annotation?
If not is there a mechanism to search for components with methods with a specified annotation?

#Valid vs #Validated in Spring Boot Rest API #RequestBody [duplicate]

This question already has answers here:
Difference between #Valid and #Validated in Spring
(6 answers)
Closed 5 years ago.
I am coming from a .NET background in developing REST APIs. Now working on java REST project using spring boot.
First my validation at the controller #RequestBody just stop working. While trying to fix it, I saw different ways to implement. So what would be the correct way to annotate the #RequestBody?
#Validated #RequestBody
or
#Valid #RequestBody
There are generally no huge difference between them two, #Valid is from JSR-303 standand, #Validated is spring standard. according to spring document:
Spring provides a Validator interface that can be used for validation in all layers of an application. In Spring MVC you can configure it for use as a global Validator instance, to be used whenever an #Valid or #Validated controller method argument is encountered, and/or as a local Validator within a controller through an #InitBinder method. Global and local validator instances can be combined to provide composite validation.
Reference: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-validation
However there are differences, one of them is for example, if you need use group in your validation annotation, you need to use #Validated, it is not supported with #Valid.
#Valid is in the JSR-Specification and #Validated is from spring framework.
When your program should be compatible to EJB/JSR Standard use #Valid otherwise you can use both.

How to add a custom annotation to Spring MVC?

Can anyone explain what I need to do to implement my own annotation that would add functionality to my web requests?
For example:
#Controller
public class MyController {
#RequestMapping("/abc")
#RequiresSomeSpecialHandling
public void handleSecureRequest() {
}
}
Here #RequiresSomeSpecialHandling would be my own annotation that causes some special work to be done before or after the given web request /abc.
I know that on a very high level I would need to write a bean post processor, scan classes for my annotations, and inject custom mvc interceptors when needed. But are there any shortcuts to simplify this task? Especially for the two examples above.
Thanks in advance,
This kind of Annotations, (that add additional functionality when invoking a method) looks like annotations that trigger an AOP Advice.
#see Spring Reference Chapter 7. Aspect Oriented Programming with Spring
The idea is to use the Annotation to trigger the AOP Advice.
like:
#Pointcut("#target(com.example.RequiresAuth)")
Depends on what you want to do as a result of #RequiresSomeSpecialHandling. E.g. do you want it to influence request mappings or the invocation of the method (i.e. resolving method arguments, processing the return value)?
The support for annotated classes in Spring 3.1 became much more customizable. You can browse some examples in this repo.
Also keep in mind that a HandlerInterceptor in Spring 3.1 can cast the handler Object to HandlerMethod, which gives you access to the exact method including its annotations. That may be enough for what you need to do.
If caching is one of your goals, take a look at the #Cacheable annotation (and its siblings #CachePut, #CacheEvict and #Caching), available as of Spring 3.1.

spring mvc annotation #RequestAttribute similar to #RequestParam

I would like add an annotation similar to #RequestParam, though have it pull the values from the request attribute rather than the request param...
Is there an example or explanation how to create my own annotation for this and the handler / binder needed as well?
Thanks
The blog entry with the title "Extending Spring MVC's annotation controller" answers your question. Google it to find it since Stackoverflow won't let me create the direct link.
Basically you create an #RequestAttribute annotation and then a custom WebArgumentResolver.
The blog entry has examples for #RequestAttribute and #SessionAttribute.
The svn directory with the examples is here.
http://impala-extensions.googlecode.com/svn/trunk/impala-extension-mvc/src/org/impalaframework/extension/mvc/
Since Spring 4.3 #RequestAttribute annotation is a part of Spring MVC, so there is no need to create your own #RequestAttribute annotation

Resources