java - jaxb 使用零参数构造函数进行解码

标签 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>

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

最佳答案

构造函数问题

While it does that it always chooses to use the zero parameter constructor and so all information in the xml gets lost.

因为您有一个零参数构造函数和非最终字段或公共(public)访问器,所以您在这里没问题,JAXB 可以使用它们来完成您需要它做的一切。如果您没有零参数构造函数,则需要考虑使用 XmlAdapter


真正的问题

您没有正确映射 namespace 限定。您需要确保正确映射 namespace 限定。这可以通过在名为 package-info` 的类上使用 @XmlSchema 注释来完成。

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

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

了解更多信息

我在我的博客上写了更多关于 JAXB 和 namespace 限定的内容:

关于java - jaxb 使用零参数构造函数进行解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27357177/

相关文章:

JavaFX:为节点提供固定宽度/尺寸的优化方法?

javascript - 解析输出和样式 xml 数据

java - Android - Canvas onDraw 具有跟随角色移动的缩放相机

java - 第二个结果集给出错误

c# - 如何将 xmlnamespace 添加到 xmldocument

java - Jaxb:带注释的类中的属性固定值

xml - xsd :dateTime without timezone and its conversion to Date 的语义

java - 使用 java 解析 SOAP/XML 时出现问题

java - JPanel 不响应 KeyListener 事件

xml - 如何在 Visual Basic .NET 中从 Internet 获取 "Read"XML?