未找到将POJO列表序列化为JSON的java MessageBodyWriter
我创建了一个REST服务,当返回类型为@Produce(MediaType.APPLICATION_XML)
时,该服务可以正常工作,但当我指定@Produce(MediaType.APPLICATION_JSON)
时,会收到以下错误消息:
MessageBodyWriter not found for media type=application/json, 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
还有一件事要补充,我不使用网络。xml,我使用一个应用程序类来配置其余资源
这些是我的课程:
@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 楼答案
我设法通过以下方法解决了这个问题this solution:使用jackson库代替moxy
我把它留在这里,以防其他人也面临同样的问题