JAX-RS: Returning A List Of Instances, Problem and Solution

Wrapping a list of instances with a Response:


    @GET
    public Response workshops() {
        List<Workshop> workshops = ...//a list of entities
        return Response.ok(workshops).build();
    }


Leads to a type loss carried by the Collection and the following (or similar) exception:


MessageBodyWriter not found for media type=application/json, type=class java.util.Arrays$ArrayList, 
genericType=class java.util.Arrays$ArrayList

JAX-RS comes with GenericEntity which carries the generic type. You only have to wrap the Collection with the GenericEntity to solve the problem:


import javax.ws.rs.core.GenericEntity;
//...
  @GET
    public Response workshops() {
        List<Workshop> workshops = ...//a list of entities
        GenericEntity<List<Workshop>> l
        ist = new GenericEntity<List<Workshop>>(workshops) {
        };
        return Response.ok(list).build();
    }

See you at Java EE Workshops at Munich Airport, particularly at: Effective Java EE 7!


Cloudy Jakarta EE and MicroProfile: Microservices, Clouds and Beyond Jakarta EE / MicroProfile airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

Nice tips. You can also use Jackson (in Jersey you have to register JacksonFeature) and Collections are managed without wrapping

Posted by Frederic Renout on October 24, 2014 at 07:11 AM CEST #

If you use this way with Jersey, the generated WADL does not include the type information. It will just say something like <representation mediaType="application/json"/>

Which, I guess, is an improvement, because without the GenericEntity, Jersey generates an incorrect mapping which ignores that it's supposed to be a collection:

<response><ns2:representation element="fooItem" mediaType="application/json"/></response>

If you then use this generated WADL to generated a JAXWS client, well, all of your bindings end up being invalid.

Posted by Nim on June 07, 2015 at 11:43 AM CEST #

Thanks Bro, this solve my problem.

Posted by 114.143.6.98 on March 03, 2016 at 07:52 PM CET #

O my god you are my savior! Thanku thanku thanku

Posted by manu on March 26, 2016 at 08:29 PM CET #

WOW, thanks bro ! you save my life.

Posted by kien tran on April 18, 2016 at 05:18 PM CEST #

thanks.. this was awesome..

Posted by anil on August 19, 2016 at 09:05 PM CEST #

Hi Adam.

What about having a cyclic ref in Workshop class and providing a MessageBodyWriter<Workshop> for inflating this cyclic ? It means GenericEntity won't use the MessageBodyWriter<Workshop>... have you seen something like that before?

Posted by Julien on August 12, 2017 at 03:00 PM CEST #

Hi folks.
I've doing that implementation on the client side, and it worked for me. Thanks guys.

Posted by Cleberson Pedreira Pauluci on December 21, 2017 at 12:37 PM CET #

Hi Adam,
strangely I am facing the problem that List, when they are part of the an Object, as property, they aren't returned to the client side. Could this result to the above mentioned problem?
The use case is:
Object A has a List as property and is know from Service 1 and 2, 1 calling 2 to get Object A.
Calling directly only 2, one gets a complete Object A with all its property.
But Calling 2 through 1, one gets the Object A with all its property except the List property.

Regards,
D.

Posted by Daniel Wamara on January 23, 2019 at 01:39 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License