首页 文章

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

提问于
浏览
0

我做了一个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

    我以某种方式设法通过遵循this solution来解决问题:其中使用jackson库而不是moxy .

    我会留在这里,以防其他人面临同样的问题 .

相关问题