Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Spring annotation for request parameters

I would like to write custom annotations, that would modify Spring request or path parameters according to annotations. For example instead of this code:

@RequestMapping(method = RequestMethod.GET) public String test(@RequestParam("title") String text) {    text = text.toUpperCase();    System.out.println(text);    return "form"; } 

I could make annotation @UpperCase :

@RequestMapping(method = RequestMethod.GET)    public String test(@RequestParam("title") @UpperCase String text) {    System.out.println(text);    return "form"; } 

Is it possible and if it is, how could I do it ?

like image 231
arminas Avatar asked Jun 08 '15 17:06

arminas


People also ask

What is @RequestParam annotation in spring boot?

@RequestParam is a Spring annotation used to bind a web request parameter to a method parameter. It has the following optional elements: defaultValue - used as a fallback when the request parameter is not provided or has an empty value. name - name of the request parameter to bind to.

How do I send parameters in spring boot request?

Spring Boot made passing parameters easy with Java annotations. In the above URL, there are two parameters which are v and t. To pass the parameters, put “?”. Then, add the parameter name followed by “=” and the value of the parameter.

What is the use of @param annotation?

In Spring MVC, the @RequestParam annotation is used to read the form data and bind it automatically to the parameter present in the provided method. So, it ignores the requirement of HttpServletRequest object to read the provided data.


1 Answers

As the guys said in the comments, you can easily write your annotation driven custom resolver. Four easy steps,

  1. Create an annotation e.g.

@Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface UpperCase {     String value(); } 
  1. Write a resolver e.g.

public class UpperCaseResolver implements HandlerMethodArgumentResolver {      public boolean supportsParameter(MethodParameter parameter) {         return parameter.getParameterAnnotation(UpperCase.class) != null;     }      public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,             WebDataBinderFactory binderFactory) throws Exception {         UpperCase attr = parameter.getParameterAnnotation(UpperCase.class);         return webRequest.getParameter(attr.value()).toUpperCase();     } } 
  1. register a resolver

<mvc:annotation-driven>         <mvc:argument-resolvers>             <bean class="your.package.UpperCaseResolver"></bean>         </mvc:argument-resolvers> </mvc:annotation-driven> 

or the java config

    @Configuration     @EnableWebMvc     public class Config extends WebMvcConfigurerAdapter {     ...       @Override       public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {           argumentResolvers.add(new UpperCaseResolver());       }     ...     } 
  1. use an annotation in your controller method e.g.

public String test(@UpperCase("foo") String foo)  
like image 59
Master Slave Avatar answered Sep 21 '22 03:09

Master Slave