Java 使用JAXB在JSON元素上显示toString
我开始学习java+mongodb,所以我开始做restfull,它的所有工作,但我的Cliente类上有一个属性,我想定制他的反序列化,“私有ObjectId”,这是我的类:Java 使用JAXB在JSON元素上显示toString,java,json,mongodb,rest,jaxb,Java,Json,Mongodb,Rest,Jaxb,我开始学习java+mongodb,所以我开始做restfull,它的所有工作,但我的Cliente类上有一个属性,我想定制他的反序列化,“私有ObjectId”,这是我的类: import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRoo
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import org.bson.types.ObjectId;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Cliente {
private ObjectId id;
private String nome;
private String cpf;
private String rg;
//methods…
}
当我进行反序列化时,我得到以下结果:
{
"id": {
"timestamp": 1537705891,
"machineIdentifier": 13207740,
"processIdentifier": -17673,
"counter": 3768415,
"time": 1537705891000,
"date": 1537705891000,
"timeSecond": 1537705891
},
"nome": "Kathia",
"cpf": "470.428.859-53",
"rg": "25.876.962-8"
}
但我喜欢这样:
{
"id": "ObjectId('5ba7fdf1c988bcbaf7398060')";
"nome": "Kathia",
"cpf": "470.428.859-53",
"rg": "25.876.962-8"
}
或
如果你的JSON由Jackson处理,有人可以帮我吗 然后您可以装备
Cliente
类的ObjectId
属性
使用自定义JSON序列化程序和反序列化程序
通过使用和对其进行注释
序列化程序将ObjectId
实例转换为字符串,例如。
“ObjectId('5BA7FDF1C988BCBAFF7398060')”
通过或方法:
在JSON示例中,它应该是
,
,而不是代码>,不是吗?我正在使用jaxb。
{
"id": "5ba7fdf1c988bcbaf7398060";
"nome": "Kathia",
"cpf": "470.428.859-53",
"rg": "25.876.962-8"
}
@JsonSerialize(using = ObjectIdSerializer.class)
@JsonDeserialize(using = ObjectIdDeserializer.class)
private ObjectId id;
import java.io.IOException;
import org.bson.types.ObjectId;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class ObjectIdSerializer extends StdSerializer<ObjectId> {
public ObjectIdSerializer() {
super(ObjectId.class);
}
@Override
public void serialize(ObjectId value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeString("ObjectId('" + value.toString() + "')");
}
}
import java.io.IOException;
import org.bson.types.ObjectId;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class ObjectIdDeserializer extends StdDeserializer<ObjectId> {
public ObjectIdDeserializer() {
super(ObjectId.class);
}
@Override
public ObjectId deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
String s = parser.getValueAsString();
try {
String[] parts = s.split("'");
String hexString = parts[1]; // get the part between 1st ' and 2nd '
return new ObjectId(hexString);
} catch (RuntimeException e) {
throw new JsonParseException(parser, "parsing error", e);
}
}
}