我有一份我需要编组的列表地图.我创建了XML适配器,但java.util.List is an interface, and JAXB can't handle interfaces.在创建JAXB上下文时我一直在努力.我该如何编组列表地图?
这是我的代码:
@XmlRootElement(name = "myClass")
public class MyClass {
@XmlJavaTypeAdapter(MapOfListsAdapter.class)
protected Map<Integer, List<Condition>> expectedResults;
Run Code Online (Sandbox Code Playgroud)
我为Map编写了适配器MapOfListsAdapater:
public class MapOfListsAdapter extends XmlAdapter<List<MapOfListsEntry>, Map<Integer, List<Condition>>> {
@Override
public List<MapOfListsEntry> marshal(Map<Integer, List<Condition>> v) {...}
@Override
public Map<Integer, List<Condition>> unmarshal(List<MapOfListsEntry> v) {...}
}
Run Code Online (Sandbox Code Playgroud)
MapOfListEntry具有以下JAXB注释:
public class MapOfListsEntry {
@XmlAttribute
private Integer key;
@XmlElementRef
@XmlElementWrapper
private List<Condition> value;
Run Code Online (Sandbox Code Playgroud) 我需要返回几个结果和结果总数的客户列表。我必须在具有不同实体的几个地方执行此操作,因此我想要一个具有这两个属性的通用类:
@XmlRootElement
public class QueryResult<T> implements Serializable {
private int count;
private List<T> result;
public QueryResult() {
}
public void setCount(int count) {
this.count = count;
}
public void setResult(List<T> result) {
this.result = result;
}
public int getCount() {
return count;
}
public List<T> getResult() {
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
和服务:
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public QueryResult<TestEntity> findAll(
QueryResult<TestEntity> findAll = facade.findAllWithCount();
return findAll;
}
Run Code Online (Sandbox Code Playgroud)
实体不重要:
@XmlRootElement
public class TestEntity implements Serializable {
...
}
Run Code Online (Sandbox Code Playgroud)
但这会导致: javax.xml.bind.JAXBException: class test.TestEntity nor …
JSON在泽西岛使用杰克逊处理器时,何时以及为什么我需要JAXB在两者之间使用注释?Object->JAXB->JSON
杰克逊还提供自己的JAX-RS直接供应商Object->JSON.这种方法缺少什么?或者为什么我更喜欢另一个
ps:我也用春天