Fouzi Otoman
Fouzi Otoman

Reputation: 56

MessageBodyWriter not found for serializing list of POJOs into JSON

I made a REST Service that works fine when the type of return is @Produce(MediaType.APPLICATION_XML), but when I specify @Produce(MediaType.APPLICATION_JSON) I get the following error message:

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

I've already try this: adding jersey-media-jackson and jersey-jackson-moxy but it didn't work.

Environment: I'm using Glassfish 4.1.1 as application server and Netbeans 8.2 as IDE.

One another thing to add, i don't use web.xml, i'm using an Application class to configure the REST ressources.

These are my classes:

@XmlRootElement( name = "Node")
public class Sensor implements Serializable{
    protected int nwkAddr;
    protected DeviceType deviceType;
    protected String modelIdentifier;
    protected IEEEAddress ieeeAddr;
    protected String name;
    protected int parentNwkAddr;
    protected int appVersion;
    protected int manufacturerCode;
    protected List<EndPoint> endPoints;
    protected List<Link> links;
    protected transient List<Binding> bindings;
    protected transient int linkQuality;
    protected transient int reason;

    // constructors, getters and setters follow...

}

This is my Service (I am calling the getSensors() method):

@Path("sensors") 
public class SensorsResources {

    @Context
    private UriInfo context;

    @Path("zplugs")
    public ZplugResource getZplugResource(){
        return new ZplugResource();
    }

    @Path("zrcs")
    public ZrcResource getZrcResource(){
        return new ZrcResource();
    }

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    public List<Sensor> getSensors(){
        System.out.println("getSensors");   
        return NodeBdd.getListOfMyNodes();
    }
}

And this is a class where I use a static attribute, just to simulate data.

public class NodeBdd {
    private static List<Sensor> listOfMyNodes = new ArrayList<>();

    static{
        listOfMyNodes.add(new Sensor(1, DeviceType.COORDINATOR, "modelidentifier1", IEEEAddress.NULL_IEEE_ADDR, "UBEE", 0, 0, 0, null, null, null, 0, 0));
        listOfMyNodes.add(new Sensor(2, DeviceType.END_DEVICE, "modelidentifier2", IEEEAddress.UNKNOWN_IEEE_ADDR, "ZPLUG", 1, 1, 1, null, null, null, 1, 1));
        listOfMyNodes.add(new Sensor(3, DeviceType.END_DEVICE, "modelidentifier3", IEEEAddress.UNKNOWN_IEEE_ADDR, "ZRC", 2, 2, 2, null, null, null, 2, 2));
        listOfMyNodes.add(new Sensor(4, DeviceType.END_DEVICE, "modelidentifier4", IEEEAddress.UNKNOWN_IEEE_ADDR, "ZPLUG", 4, 4, 4, null, null, null, 4, 4));
        listOfMyNodes.add(new Sensor(5, DeviceType.END_DEVICE, "modelidentifier5", IEEEAddress.UNKNOWN_IEEE_ADDR, "ZRC", 5, 5, 5, null, null, null, 5, 5));
    }

    public static List<Sensor> getListOfMyNodes() {
        return listOfMyNodes;
    }

    public static List<Sensor> getSpecificListOfNodes(String type){
        List<Sensor> specificList = new ArrayList<>();

        for (Sensor current : listOfMyNodes) {
            if(type.equalsIgnoreCase(current.getName())){
                specificList.add(current);
            }
        }

        return specificList;
    }
}

Upvotes: 0

Views: 516

Answers (1)

Fouzi Otoman
Fouzi Otoman

Reputation: 56

I somehow managed to solve the problem by following this solution : where the jackson library is used instead of moxy.

I'll leave it here, in case anyone else faces the same problem.

Upvotes: 0

Related Questions