栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Spring中编写JSON解串器或对其进行扩展的正确方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Spring中编写JSON解串器或对其进行扩展的正确方法

我进行了很多搜索,到目前为止,找到最好的方法是在这篇文章上:

类进行序列化

package net.sghill.example;import net.sghill.example.UserDeserializerimport net.sghill.example.UserSerializerimport org.prehaus.jackson.map.annotate.JsonDeserialize;import org.prehaus.jackson.map.annotate.JsonSerialize;@JsonDeserialize(using = UserDeserializer.class)public class User {    private ObjectId id;    private String   username;    private String   password;    public User(ObjectId id, String username, String password) {        this.id = id;        this.username = username;        this.password = password;    }    public ObjectId getId()       { return id; }    public String   getUsername() { return username; }    public String   getPassword() { return password; }}

反序列化器类

package net.sghill.example;import net.sghill.example.User;import org.prehaus.jackson.JsonNode;import org.prehaus.jackson.JsonParser;import org.prehaus.jackson.ObjectCodec;import org.prehaus.jackson.map.DeserializationContext;import org.prehaus.jackson.map.JsonDeserializer;import java.io.IOException;public class UserDeserializer extends JsonDeserializer<User> {    @Override    public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {        ObjectCodec oc = jsonParser.getCodec();        JsonNode node = oc.readTree(jsonParser);        return new User(null, node.get("username").getTextValue(), node.get("password").getTextValue());    }}

编辑:或者你可以看看这篇文章,它使用com.fasterxml.jackson.databind.JsonDeserializer的新版本。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/413641.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号