问题描述
免责声明:我对这一切都不熟悉,所以我的术语可能是错误的
Disclaimer: I'm new to all this, so my terminology may be wrong
我有一些Java POJO我希望序列化为JSON& XML。我正在使用MOXy 2.5.0 for JSON和Jersey 2.4.1。
I've got some Java POJO's I want to serialize to JSON & XML. I'm using MOXy 2.5.0 for JSON and Jersey 2.4.1.
@XmlRootElement
class Root {
// @XmlElements({@XmlElement(name = "destination_address", type = LatLong.class),
// @XmlElement(name = "destination_address", type = Polygon.class)})
public Object[] destination_addresses;
}
public class LatLong {
public double lat, lng;
}
public class Polygon {
protected List<LatLong> points = new ArrayList<LatLong>();
@XmlElements({@XmlElement(name = "lat", type = Lat.class),
@XmlElement(name = "lng", type = Lng.class)})
private LatOrLong[] getLatOrLongs() {
LatOrLong[] retval = new LatOrLong[points.size() * 2];
for (int point = 0; point < points.size(); point++) {
LatLong latLong = points.get(point);
retval[point * 2] = new Lat(latLong.lat);
retval[point * 2 + 1] = new Lng(latLong.lng);
}
return retval;
}
static abstract private class LatOrLong {
@XmlValue
private double latOrLong;
private LatOrLong() {}
private LatOrLong(double latOrLong) {this.latOrLong = latOrLong;}
}
static private class Lat extends LatOrLong {
private Lat() {}
private Lat(double lat) {super(lat);}
}
static private class Lng extends LatOrLong {
private Lng() {}
private Lng(double lng) {super(lng);}
}
}
这在注释掉两行的XML中不起作用,但在JSON中,MOXy将类型:latLong
属性添加到 destination_addresses
数组,以及使用 Polygon
的 toString()
方法。
This doesn't work in XML with the two lines commented out, but in JSON, MOXy is adding a type: latLong
attribute to the destination_addresses
array, as well as using the toString()
method of Polygon
.
- 如何隐藏
类型
? - 如何让MOXy使用
getLatOrLongs()
而不是toString()
?
- How can I hide the
type
? - How can I get MOXy to use
getLatOrLongs()
instead oftoString()
?
编辑:我已将 Polygon
简化为序列化点
并将 destination_addresses
更改为列表< Object>
而不是对象[]
。
I've simplified Polygon
to just serialize points
and changed destination_addresses
to be a List<Object>
instead of Object[]
.
推荐答案
默认情况下,使用MOXy启用pojo映射功能。
The pojo mapping feature is by default enabled using MOXy.
但是,无论如何,你需要实现一个特定的marshal / unmarshal(我从MongoDB这里拿到ObjectID,它位于:
But if for any case, you need to implement a specific marshal/unmarshal (I take here ObjectID from MongoDB which is a t:
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.bson.types.ObjectId;
public class ObjectIdXmlAdapter extends XmlAdapter<String, ObjectId> {
@Override
public String marshal(ObjectId id) throws Exception {
if(id == null) {
return null;
} else {
return id.toString();
}
}
@Override
public ObjectId unmarshal(String id) throws Exception {
return new ObjectId(id);
}
}
然后,在您的POJO上:
And then, on your POJO:
@XmlJavaTypeAdapter(ObjectIdXmlAdapter.class)
public ObjectId getId() {
return id;
}
将按预期序列化您的id元素...
Will serialize your id element as expected...
希望这会有所帮助,这应该是主要的麻烦。
Hope this helps, this should be the main trouble.
这篇关于MOXy添加类型并使用toString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!