java - @Produces collection in JAXRS / RestEasy -
i found strange behaviour cannot understand.
i have tested 4 similar examples:
1
@get @produces(mediatype.application_json) public response produce() { list<book> books = arrays .aslist(new book[] { new book("aaa", "aaa", "12345"), new book("bbb", "bbb", "09876") }); return response.ok(books).build(); }
2
@get @produces(mediatype.application_json) public list<book> produce() { list<book> books = arrays .aslist(new book[] { new book("aaa", "aaa", "12345"), new book("bbb", "bbb", "09876") }); return books; }
3
@get @produces(mediatype.application_xml) public list<book> produce() { list<book> books = arrays .aslist(new book[] { new book("aaa", "aaa", "12345"), new book("bbb", "bbb", "09876") }); return books; }
4
@get @produces(mediatype.application_xml) public response produce() { list<book> books = arrays .aslist(new book[] { new book("aaa", "aaa", "12345"), new book("bbb", "bbb", "09876") }); return response.ok(books).build(); }
everything works in #1, #2, #3 4th example throws:
could not find messagebodywriter response object of type: java.util.arrays$arraylist of media type: application/xml.
i run on wildfly 9 , wonder if related resteasy or jaxrs in general? know can fix wrapping collection in genericentity, don't understand inconsistent behaviour.
the problem missing of type information. required jaxb, handles xml serialization.
1 , 2 works because jackson being used json, , doesn't need know type information introspects properties.
3 works because type information known through method return type.
4 doesn't work because there no type information. it's erased type erasure. that's genericentity
comes rescue. stores type information.
normally type erasure removes generic type information such
response
instance contains, e.g., entity of typelist<string>
appears contain rawlist<?>
@ runtime. when generic type required select suitablemessagebodywriter
, class may used wrap entity , capture generic type.
Comments
Post a Comment