Hi all,
We are using cxf with Spring at work, and I have a newbie question...
This is my method signature:
@GET
@Path("foo")
public Response foo(@MatrixParam("l") List<String> myList) {
>From that, I want to get a list back with the initial input String (to
my matrix parameter l) being split into list elements at any comma,
i.e. I want e.g. ";l=a,b,c" to turn into a list of three elements: a,
b and c.
My converter below is registered (a breakpoint in it is triggered for
myList), but instead of passing rawType as List, I get rawType as
String (so it doesn’t do anything).
public class StringListHandler implements ParamConverterProvider {
@Override
public <T> ParamConverter<T> getConverter(final Class<T> rawType,
Type genericType, Annotation[] annotations) {
if(rawType == List.class) {
return new ParamConverter<T>() {
@Override
public T fromString(String value) {
return
rawType.cast(Arrays.asList(value.split("\\s*,\\s*")));
}
@Override
public String toString(T value) {
return value.toString();
}
};
}
return null;
}
}
Interestingly enough, I do get a list back. But instead of three
elements a, b and c, it only seems to have one: a,b,c
What piece of JAX-RS/cxf voodoo am I missing to make this work? ;-)
Is it maybe because cxf comes with a default implementation for a
List<String> converter so it can turn a URL like foo?l=a&l=b&l=c into
a List<String> ? Does that also get called for foo;l=a;l=b;l=c ? (I
thought that c would overwrite a in that situation?)
Do I have to use a custom class with a List<String> property, like
class MyContainer {
public List<List> l;
}
and change the method signature from above to
public Response foo(@MatrixParam("l") MyContainer myContainer) {
then check for MyContainer.class in ParamConverterProvider and change
the fromString() method to
public T fromString(String value) {
MyContainer mC = new MyContainer();
mc.l = Arrays.asList(value.split("\\s*,\\s*"));
return rawType.cast(mc);
}
Or should I create a custom argument annotation, say @CommaSeparated,
and have ParamConverterProvider check for that (and
rawType==String.class)?
But if I do that, won't I get a List<List<String>> back?
Any help much appreciated!
Kind regards,
Christian