我有:
@XmlRootElement( name = "list" )
@XmlAccessorType(XmlAccessType.FIELD)
public class JaxbList<T> implements Serializable{
private static final long serialVersionUID = -7054818420430302896L;
@XmlAnyElement
private Collection<T> list;
}
和
@XmlRootElement(name="payload")
public class Payload<T> implements Serializable {
private static final long serialVersionUID = -2919689796111868862L;
@XmlTransient
private T data;
}
和
@XmlRootElement (name="data")
public class PayloadData<T> implements Serializable {
@XmlTransient
private T value;
@XmlTransient
private URI uri;
public PayloadData() {}
@XmlAnyElement (lax = true )
@XmlMixed
public T getValue() {
return value;
} ...
}
我正在使用的是这样的:
@GET
public Response getWorkspaces(@Context UriInfo uriInfo) {
JaxbList<PayloadData<WorkspaceInfo>> workspaces = new JaxbList<PayloadData<WorkspaceInfo>> ( new ArrayList<PayloadData<WorkspaceInfo>>( dbData) );
return Response.ok().entity( new Payload( workspaces ) ).build();
}
GET端点的JSON输出不正确。 XML很好。 XML输出:
<payload>
<list>
<data>
<uri>http://localhost:8080/api/rest/v1/workspaces/1/items</uri>
<data>
<id>1</id>
<category>Cat1</category>
<label>WKS1</label>
</data></data>
<data>
<uri>http://localhost:8080/api/rest/v1/workspaces/2/items</uri>
<data>
<id>2</id>
<category>Cat2</category>
<label>WKS2</label>
</data>
</data>
</list>
</payload>
错误的JSON是:
{
"list":{
"data":{
"uri":"http://localhost:8080/api/rest/v1/workspaces/1/items.json",
"data":{
"id":1,
"category":"Cat1",
"label":"WKS1"
}
},
"data":{
"uri":"http://localhost:8080/api/rest/v1/workspaces/2/items.json",
"data":{
"id":2,
"category":"Cat2",
"label":"WKS2"
}
}
}
}
您可以看到,“data”元素不是创建JSON数组,而只是一系列具有相同键“data”的对象,而不是包含在 [] 中的json数组。 即,一种可能的预期JSON输出:
{
"list":[
{"data":{
"uri":"http://localhost:8080/api/rest/v1/workspaces/1/items.json",
"data":{
"id":1,
"category":"Cat1",
"label":"WKS1"
}
}
},
{"data":{
"uri":"http://localhost:8080/api/rest/v1/workspaces/2/items.json",
"data":{
"id":2,
"category":"Cat2",
"label":"WKS2"
}
}
}
]
}
任何想法可能在这里出错?