0

I have a Spring MVC controller. And I have in the method 50 parameters. All of the parameters have very specific name, for example: FOO[]. I don't want write 50 parameters in the method signature like this:

@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public String test(
    @RequestParam(value = "FOO[]") String foo,
    @RequestParam(value = "BAR[]") String bar,
// Other 48 parameters
)
{
    return "test";
}

I want to map all the parameters on the one object, I mean, I want to write a simple bean class with getter/setter and use it like method parameter. But how can I set custom names to my class fields?

e.g.:

class FooBar {
    @SomeAnnotation_for_binding_the_field_to_my_field_FOO[]
    private String foo;
    private String bar;
    // Other 48 fields
    // getters/setters
}

3 Answers 3

1

I know annotations are kinda cool, but think rationally. You HAVE to enumerate, in code, all the mappings. There is nothing implicit about mapping FOO[] to foo, it seems to be beyond your control. Just take the parameters as a map (you can always ask Spring to give you map of all parameters) and do something like:

@RequestMapping
public String test(@RequestParam Map<String, Object> map) {
    MyObject mo = new MyObject();
    mo.setFoo(map.get("FOO[]").toString());
    mo.setBar(map.get("WOBBLE13[][]").toString);
    return "whatever";
}

If you want to make this process more automatic, and if there exists an alorithm that maps parameter names to property names, you can use Spring's bean wrapper:

@RequestMapping
public String test(@RequestParam Map<String, String> map) {
   BeanWrapper bw = new BeanWrapperImpl(new MyObject);
   for (Entry<String, Object> entry : map.entrySet()) {
       bw.setProperty(entry.getKey(), entry.getValue());
   }
}

private static String decodeName(String n) {
    return n.toLowerCase().substring(0,n.length() - 2);
}

You could make the process even more automatic by using a different Binder, you could (really, not a problem) add some custom annotations... but really, there is no point, if you just have a single case of 50 params. If you insist, add a comment.

Sign up to request clarification or add additional context in comments.

Comments

0

THis sounds like a good time to use a hashmap, with key as the var name and value as value. Wrap that in a form backing object.

Comments

0

You could have a resource class i.e FooBarResource.jave and in your controller use that as a request body something like the following:

@ResponseBody
@RequestMapping(value = "/test", method = RequestMethod.POST)
@Secured({"ROLE_ADMIN"})
    public ResponseEntity<ModelMap> createTest(@Valid @RequestBody FooBarResource body, UriComponentsBuilder builder) {

2 Comments

And how can I named my field FOO[] in Java? :)
you had a FooBar class all i done just renamed it :) you could use the same class as your resource class. If you wanted any fields in the controller you just do body.getFoo() for example. you could also do things like @NotNull(message = "foo is mandatory") on foo in your resource class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.