使用JAXB在JSON元素上显示toString

时间:2018-09-28 03:21:32

标签: java json mongodb rest jaxb

我开始研究java + mongodb,因此我开始进行一次restfull的所有工作,但是Cliente类上有一个属性,我想自定义他的反序列化,即“私有ObjectId id”,这是我的课程:

declare @t as table(
  [num] int
);

insert into @t([num])
select 13920 union all
select 13921 union all
select 13922 union all
select 13923 union all
select 13924;

declare @base as date = '1980-01-01';

select convert(varchar(10), dateadd(day, [num], @base), 101) as [date]
from @t;

当我进行反序列化时,我会得到:

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"
}

有人可以帮我,拜托?

1 个答案:

答案 0 :(得分:0)

如果您的JSON由Jackson处理 那么您可以为ObjectId id类的Cliente属性装备 使用自定义JSON序列化器和反序列化器 通过用@JsonSerialize@JsonDeserialize进行注释。

@JsonSerialize(using = ObjectIdSerializer.class)
@JsonDeserialize(using = ObjectIdDeserializer.class)
private ObjectId id;

您的序列化程序会将ObjectId实例转换为字符串,例如 "ObjectId('5ba7fdf1c988bcbaf7398060')"通过ObjectId.toHexString()ObjectId.toString()方法:

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() + "')");
    }
 }

同样,您的解串器会将String转换为例如 "ObjectId('5ba7fdf1c988bcbaf7398060')"ObjectId实例 通过ObjectId(String hexString)构造函数。

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);
        }
    }
}