找不到用于将POJO列表序列化为JSON的MessageBodyWriter

时间:2018-02-28 14:08:27

标签: java json rest jackson jax-rs

我提出的REST服务在返回类型为@Produce(MediaType.APPLICATION_XML)时工作正常,但是当我指定@Produce(MediaType.APPLICATION_JSON)时,我收到以下错误消息:

  

找不到媒体类型= application / json的MessageBodyWriter,   type = class java.util.ArrayList,genericType = java.util.List

我已经尝试过这个:adding jersey-media-jackson and jersey-jackson-moxy但它没有用。

环境:我使用 Glassfish 4.1.1 作为应用服务器, Netbeans 8.2 作为IDE。

要添加的另一件事,我不使用web.xml,我正在使用Application类来配置REST资源。

这些是我的课程:

@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...

}

这是我的服务(我正在调用getSensors()方法):

@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();
    }
}

这是一个我使用静态属性的类,只是为了模拟数据。

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;
    }
}

1 个答案:

答案 0 :(得分:0)

我设法通过跟随this solution来解决问题:使用jackson库而不是moxy。

我会把它留在这里,以防其他人遇到同样的问题。