Spring 3 MVC - Map request parameters with a prefix to a single bean

I have the following GET request:

/api/search?filter.operation=Ping&filter.namespace=

Note that parameter names include the prefix ( filter. , Filter. ).

Then I have the following bean that I want to use to get these parameters:

 class MessageSearchFilter { String operation; String namespace; ... } 

And the handler method has the following signature:

public @ResponseBody String searchMessages(MessageSearchFilter filter, ...);

However, this does not work as Spring MVC expects the "operation" and "namespace" attributes to be named that way. It works if I change my request to use "operation" and "namespace" (without the prefix "filter.").

Is there any way to tell Spring to prefix the parameters with a filter?

A related side question: what happens if I have a method signature with multiple form objects with names of colliding properties?

+8
spring spring-mvc spring-3
source share
2 answers

Adding a Method to Your Controller

 @Override protected String getFieldBindingPrefix() { return "filter."; } 

must do the job.

+1
source share

You should be able to display the incoming request parameters using @RequestParam , and you can fully qualify them:

 //Your @RequestMapping here... public @ResponseBody String searchMessages( @Requestparam("filter.operation") String filterOperation, @RequestParam("filter.namespace") String filterNamespace) { MessageSearchFilter messageSearchFilter = new MessageSearchFilter(); messageSearchFilter.operation = filterOperation; messageSearchFilter.namespace = filterNamespace; //do your thing here... } 

You will also notice that now you can add classifiers for other objects with property name names.

0
source share

All Articles