java - Spring framework REST multivalue request parameter empty array -
i have rest endpoint (spring-boot-1.3.0-release -> spring-core-4.2.4.release) takes multiple instance string parameter
@requestmapping(value = "/test", method = requestmethod.get) public responseentity<object> gettest(@requestparam(value = "testparam", required = false) string[] testparamarray) {} /test?testparam= => testparamarray has length 0 /test?testparam=&testparam= => testparamarray has length 2 (two empty string items)
i expected first case single empty sting element in array there none. ideas on how achieve this?
spring's stringtoarrayconverter
responsible conversion. if take @ convert
method:
public object convert(object source, typedescriptor sourcetype, typedescriptor targettype) { if (source == null) { return null; } string string = (string) source; string[] fields = stringutils.commadelimitedlisttostringarray(string); object target = array.newinstance(targettype.getelementtypedescriptor().gettype(), fields.length); (int = 0; < fields.length; i++) { string sourceelement = fields[i]; object targetelement = this.conversionservice.convert(sourceelement.trim(), sourcetype, targettype.getelementtypedescriptor()); array.set(target, i, targetelement); } return target; }
basically, takes input (empty string
in case), split comma , return array values of exploded string
. result of splitting empty string
, of course, empty array
.
when pass 2 parameters same name, arraytoarrayconverter
called, behaves expect , returns array 2 empty string
s.
if want disable default string
array
behavior, should register converter
converts empty string
single element array
.
Comments
Post a Comment