Spring MVC bean mapping to HTTP GET request parameters similar to @BeanParam parameters

In Jersey there is an @BeanParam annotation with which I can have query parameters mapped to bean attributes.

In Spring, I can only find @RequestBody , which obviously works with the request body, and not with the request parameters.

Is there a way to have request parameters mapped to a bean using Spring?

+6
source share
1 answer

Just create a Pojo Java Bean with fields with names that match your query parameters.

Then use this class as an argument to your request handler method (without any additional annotations)

 public class Example { private String x; private Integer y; //Constructor without parameter needed! public Example(){} //Getter + Setter } @Controller @RequestMapping("someUrl") public class SomeController { @RequestMapping public String someHandler (Example example) { System.out.println(example.getX()); return "redirect:someOtherUrl"; } } 
+12
source

Source: https://habr.com/ru/post/958791/


All Articles