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.

genericentity

normally type erasure removes generic type information such response instance contains, e.g., entity of type list<string> appears contain raw list<?> @ runtime. when generic type required select suitable messagebodywriter, class may used wrap entity , capture generic type.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -