Hi,
Generic types are now supported for message body readers in the
trunk. See below for code extracted from a unit test. Note the ugly
way to get the paramterized type for List<String> from the generic
interface of ListStringReader and note that listStringType.equals
needs to be used as reference equality will not work.
Support for message body writers is still pending. The solution Lars
pointed to may provide the answer :-)
Paul.
@Provider
public static class ListStringReader implements
MessageBodyReader<List<String>> {
private final Type listStringType;
public ListStringReader() {
ParameterizedType iface = (ParameterizedType)
this.getClass().getGenericInterfaces()[0];
listStringType = iface.getActualTypeArguments()[0];
}
public boolean isReadable(Class<?> c, Type t, Annotation[]
as) {
return List.class == c && listStringType.equals(t);
}
public List<String> readFrom(Class<List<String>> c, Type t,
MediaType mt, Annotation[] as,
MultivaluedMap<String, String> headers, InputStream
in) throws IOException {
return Arrays.asList(readFromAsString(in).split(","));
}
private String readFromAsString(InputStream in) throws
IOException {
Reader reader = new InputStreamReader(in);
StringBuilder sb = new StringBuilder();
char[] c = new char[1024];
int l;
while ((l = reader.read(c)) != -1) {
sb.append(c, 0, l);
}
return sb.toString();
}
}
@Path("/")
public class ListResource {
@POST public String post(List<String> ls) {
assertEquals(4, ls.size());
assertEquals(Arrays.asList("a", "b", "c", "d"), ls);
String v = "";
for (String s : ls) {
if (v.length() > 0) v += ",";
v += s;
}
return v;
}
}
On Apr 2, 2008, at 1:45 AM, FSauer_at_dsthealthsolutions.com wrote:
>
>
> How does one write JAXB entity providers for collections? Wrapping
> them in a class works fine, but
> I want to be able to return List<SomeType> from resource methods
> but I can't figure out how to register the providers.
> type-erasure makes this hard..... if not impossible. How do you
> distinguish multiple providers for ArrayList if the element type
> is not available at runtime??? Any help is appreciated,
>
> Thanks,
>
> Frank
>
>
> This e-mail and any attachments are intended only for the
> individual or company to which it is addressed and may contain
> information which is privileged, confidential and prohibited from
> disclosure or unauthorized use under applicable law. If you are not
> the intended recipient of this e-mail, you are hereby notified that
> any use, dissemination, or copying of this e-mail or the
> information contained in this e-mail is strictly prohibited by the
> sender. If you have received this transmission in error, please
> return the material received to the sender and delete all copies
> from your system.
>