我有一个 UUID 类,我从静态工厂方法中获取 uuid,例如 UUIDGenerator.getInstance().getUuid();
我有另一个将这些 UUid 作为列表的类。
class Artifact
{
Uuid uuid;
setUuid(Uuid uuid)
{
this.uuid = uuid;
}
Uuid getUuid()
{
return this.uuid;
}
}
Class ArtifactData
{
private List<Artifact> artifacts;
//setter for list
// getter for list
}
我希望将 xml 创建为
<ArtifactData>
<AssociatedArtifactList>
<ArtifactUuid>#some value<ArtifactUuid>
</AssociatedArtifactList>
</ArtifactData>
如何使用 Jaxb 注释创建此 xml。它抱怨说 Uuid 没有公共构造函数,因为它是由工厂方法构造的。
编辑:无法修改 Uuid 和 Uuid 生成器。它们在一个 JAR 中。这是我迄今为止尝试过的
public class Artifact
{
@XmlJavaTypeAdapter(ArtifactUuidAdapter.class)
private Uuid uuid;
public Uuid getUuid()
{
return uuid;
}
public void setUuid(Uuid uuid)
{
this.uuid = uuid;
}
}
@XmlRootElement
public class ArtifactData
{
private List<Artifact> associatedArtifactList;
public List<Artifact> getArtifacts()
{
return associatedArtifactList;
}
@XmlElementWrapper(name="associatedArtifactList")
@XmlElement(name = "artifactUuid")
public void setArtifacts(List<Artifact> artifacts)
{
this.associatedArtifactList = artifacts;
}
}
public class ArtifactUuidAdapter extends XmlAdapter<Uuid, String>
{
@Override
public Uuid marshal(String uuid) throws Exception
{
return Uuid.getInstance(uuid);
}
@Override
public String unmarshal(Uuid uuid) throws Exception
{
return uuid.getData();
}
}
我仍然收到一个错误,称为 no arg default constructor is missing。