How can I use SimpleFormController with Validator with Spring 3? - spring

I'm using Spring 3 and using SimpleFormController is deprecated. It sounds like we should use annotions like #Controller instead.
Okay, but how can I manage the validator for my form within my controller?
Thanks

Still some issues with 3.0RC1, see the following issue if you want to look at annotation based validation and JSR 303: http://jira.springframework.org/browse/SPR-6189

Spring 3 has shiny new support for JSR 303 validation. See the docs here.

Related

#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.

Does a Spring controller returning a ListenableFuture needs #AsynchEnable in configuration?

As of spring 4.1, spring controllers accept return value that can be of type ListenableFuture. is returning a ListenableFuture return value sufficient in making the controller async? Or does it also need #enableAsync annotation somewhere in spring configuration file or/and anything else? I am following this tutorial
I found out that what i was looking for is not #enableAsync but a servlet 3.0 property called async-supported. According to this link, spring-boot defaults async-supported to true.
Hence, there is no need of any further configuration to do if you're using spring-boot.

#Valid working without spring-mvc?

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?

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