jaxb使用零参数构造函数进行解组

时间:2014-12-08 11:37:52

标签: java xml jaxb

我有一些代码将一些xml转换为java对象。虽然它确实如此,但它总是选择使用零参数构造函数,因此xml中的所有信息都会丢失。我该怎么办?

我的jaxb Java代码

JAXBContext jaxbContext = JAXBContext.newInstance(PasswordResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
PasswordResponse pr = (PasswordResponse) jaxbUnmarshaller.unmarshal(**legit-xml-input**);

我的Java对象代码

package testhttprequest;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name="PasswordResponse", namespace = "https://mynamespace.com")
@XmlAccessorType(XmlAccessType.FIELD)
public class PasswordResponse
{
    @XmlElement(name = "date", required = true)
    public date date;
    @XmlElement(name = "type", required = true)
    public int type;
    @XmlElement(name = "version", required = true)
    public int version;
    @XmlElement(name = "error", required = true)
    public int error;
    @XmlElement(name = "status", required = true)
    public int status;
    @XmlElement(name = "password", required = true)
    public String password;

    public PasswordResponse()
    {
        date = new date();
        type = 1;
        version = 1;
        error = 1;
        status = 1;
    }

    public PasswordResponse(int day, int month, int year, int type, int version)
    {
        this.date = new date(day, month, year);
        this.type = type;
        this.version = version;
        this.error = 0;
        this.status = 200;

    }

    public testhttprequest.date getDate()
    {
        return date;
    }

    public int getError()
    {
        return error;
    }

    public String getPassword()
    {
        return password;
    }

    public int getStatus()
    {
        return status;
    }

    public int getType()
    {
        return type;
    }

    public int getVersion()
    {
        return version;
    }

    public void setDate(testhttprequest.date date)
    {
        this.date = date;
    }

    public void setError(int error)
    {
        this.error = error;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public void setStatus(int status)
    {
        this.status = status;
    }

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

    public void setVersion(int version)
    {
        this.version = version;
    }
}

最后这是我的xml:

<PasswordResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="https://mynamespace.com">
<date>
<day>1</day>
<month>1</month>
<year>1</year>
</date>
<type>1</type>
<version>1</version>
<error>0</error>
<status>200</status>
<password>000</password>
</PasswordResponse>

如何强制应用程序使用带有参数的构造函数?

1 个答案:

答案 0 :(得分:3)

构造函数问题

  

虽然它确实如此,但它总是选择使用零参数   构造函数,因此xml中的所有信息都会丢失。

由于你有一个零参数构造函数,非最终字段或公共访问器,你可以在这里使用,JAXB可以使用它们来完成你需要的所有工作。如果您没有零参数构造函数,则需要考虑使用XmlAdapter


真实问题

您没有正确映射命名空间限定。您需要确保正确映射名称空间限定。这可以使用@XmlSchema‘ annotation on a class called package-info`来完成。

@XmlSchema( 
    namespace = "https://example.com",
    elementFormDefault = XmlNsForm.QUALIFIED) 
package testhttprequest;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

了解更多信息

我在博客上写了更多关于JAXB和名称空间限定的内容: