从codehaus到fasterxml的序列化和反序列化行为是否有变化?

问题描述

我正在从 codehaus + Jboss6 => fastxml+wildfly15 迁移,但是看起来 lije 序列化和反序列化功能没有按预期工作,并且总是得到“无法反序列化的实例” 解析 json 时出现 START_ARRAY 令牌错误

相同的功能适用于 codehaus + Jboss6。

@XmlRootElement(name = "student")
@XmlAccessorType(XmlAccesstype.FIELD)
@XmlType(name = "student",propOrder = {
    "id","firstName","lastName","age","myHobbies"
})
public class Student {

    @XmlElement
    private int id;
    @XmlElement
    private String firstName;
    @XmlElement
    private String lastName;
    @XmlElement
    private int age;
    @XmlElement(name = "My-hobbies")
    private Hobbies myHobbies;

    // Must have no-argument constructor
    public Student() {

    }

    public Student(int id,String firstName,String lastName,int age,Hobbies myHobbies) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.myHobbies = myHobbies;
    }

    //removed setter and getter method
    
    @JsonIgnore
    public Hobbies getMyHobbies() {
        return myHobbies;
    }

    @JsonProperty("My-hobbies")
    public List<Hobby> getHobbiesUnwrapped() {
        if (getMyHobbies()==null) {
            this.myHobbies = new Hobbies();
        }
        return getMyHobbies().getHobby();
    }
    
    public void setHobbies(Hobbies myHobbies) {
        this.myHobbies = myHobbies;
    }

    public int getId() {
        return this.id;
    }
}

一个类:

@XmlAccessorType(XmlAccesstype.FIELD)
@XmlType(name = "Hobby",propOrder = {
    "type"})
public class Hobby {

    @XmlElement(required = true)
    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

一个有兴趣爱好的班级:

@XmlAccessorType(XmlAccesstype.FIELD)
@XmlType(name = "Hobbies",propOrder = {
    "hobby"
})
public class Hobbies {

    @Override
    public String toString() {
        return "Hobbies [hobby=" + hobby + "]";
    }

    public Hobbies() {
        super();
        this.hobby = new ArrayList<Hobby>();
    }
    
    public Hobbies(List<Hobby> hobby) {
        super();
        this.hobby = hobby;
    }

    private List<Hobby> hobby;

    @JsonIgnore
    @JsonProperty("hobbies")
    public List<Hobby> getHobby() {
        return hobby;
    }

    public void setHobby(List<Hobby> hobby) {
        this.hobby = hobby;
    }
}

这是我的控制器类:

@POST
@Path("/send")
@Consumes({"application/json","application/xml"})
public Response consumeJSON(Student student) {

    String output = student.toString();

    return Response.status(200).entity(output).build();
}

当我在邮递员中发送以下 json 请求时,抛出以下错误

无法从 START_ARRAY 令牌中反序列化 Hobbies 实例

{
    "id": 1,"firstName": "abc","lastName": "def","age": 12,"My-hobbies": [
        {
            "type": "Photography"
        },{
            "type": "Cooking"
        }
    ]
}

相同的功能适用于 codehaus + Jboss7。 任何帮助将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)