Consider the following controller method:
@RequestMapping(value = "/test", method = RequestMethod.GET)
public void test(@RequestParam(value = "fq", required = false) String[] filterQuery) {
logger.debug(fq = " + StringUtils.join(filterQuery, "|"));
}
Here is the output for different fq
combinations:
/test?fq=foo
results infq = foo
/test?fq=foo&fq=bar
results infq = foo|bar
/test?fq=foo,bar
results infq = foo|bar
/test?fq=foo,bar&fq=bash
results infq = foo,bar|bash
/test?fq=foo,bar&fq=
results infq = foo,bar|
Example 3 is the problem. I expect (want/need) it to output fq = foo,bar
.
I've tried escaping the comma with \
and using %3C
but niether work.
If I look at the HttpServletRequest
object's version:
String[] fqs = request.getParameterValues("fq");
logger.debug(fqs = " + StringUtils.join(fqs, "|"));
It prints the expected output: fqs = foo,bar
. So the "problem" is with the Spring data binding.
I could by-pass Spring's binding and use HttpServletRequest
but I really don't want to as I'm using a backing bean in my real code (same thing is happening) and don't wish to re-implement the binding functionality. I'm hoping someone can provide a simple way of preventing this behavior via escaping or some other mechanism.
TIA
UPDATE: I posted this Q on Twitter and got a reply saying the expected output appears with Spring 3.0.4.RELEASE. I've now confirmed this is the case and thus is a temporary fix. I'll go ahead and log this as a bug on the Spring JIRA system. If anyone can provide a work around or fix with 3.0.5, I'll accept their answer.
Its a hack but, have you considered passing your params delimited with '-'
Or any other delimiter maybe ~, or !,or ^, or ???
javanna already pointed out the correct root cause. I just wanted to further point out that you can also remove the StringToArrayConverter altogether as shown here and here.
I've tested your code: it's unbelievable, but I can't reproduce your issue. I've downloaded the latest version of spring (3.0.5), this is my controller:
this is my SearchRequestParams class:
and this is my simple spring configuration:
I've tested my code within tomcat 7.0.8; when I type
http://localhost:8080/testweb/test/params.htm?fq=foo,bar
I'm able to read in my log file this line:DEBUG fq = foo,bar
. What are the the differences from my code to yours? Am I doing something wrong? I'd like to help you, so if you have any doubts or if I can do some other tests for you, it will be a pleasure.UPDATE / SOLUTION
With your code I've reproduced the issue; you have the tag
<mvc:annotation-driven />
in your dispatcher servlet configuration, so you silently use a default conversion service, instance ofFormattingConversionService
, which contains a default converter fromString
toString[]
that uses comma as separator. You have to use a different conversion service bean containing your own converter fromString
toString[]
. You should use a different separator, I've choosed to use ";" because it's the separator commonly used into query string ("?first=1;second=2;third=3"):Then you have to specify this conversion service bean in your configuration:
The issue has fixed, now you should check for any side effects. I hope you don't need in your application the original conversion from
String
toString[]
(with comma as separator). ;-)As suggested by Philip Potter, I'm posting the "update" to my question as an answer, as it might've been easy to miss...
Down-grading from Spring 3.0.5.RELEASE to 3.0.4.RELEASE fixed the issue, when using the
@RequestParam
annotation, suggesting it is a bug with 3.0.5.However, it does NOT fix the related issue, when binding to a form-backing bean - which is what I have in my webapp. I've tested all version back to 3.0.0.RELEASE and get the same result (
/test?fq=foo,bar
producesfq = foo|bar
).E.g.
where
SearchRequestParams
contains a fieldString[] fq
.If anyone has a fix for this, I'll gladly accept their answer.
I have found the most elegant and the shortest way for me - add
@InitBinder
to a@Controller
:It will convert String to String[] without using separator (
null
param), with Spring class org.springframework.beans.propertyeditors.StringArrayPropertyEditor. If someone in same project will use new default conversion way, it will be ok.