4

我是 java XML 绑定的新手。

这是我的课

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Martin Burchard
 * 
 */
@XmlRootElement(name = "user")
    public class User {
    private String id;
    private String nickname;
    private String email;
    private String password;

    @XmlElement(name = "id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlElement(name = "nickname")
    public String getNickName() {
        return nickname;
    }

    public void setNickName(String nickname) {
        this.nickname = nickname;
    }

    @XmlElement(name = "email")
    public String getEMail() {
        return email;
    }

    public void setEMail(String email) {
        this.email = email;
    }

    @XmlElement(name = "password")
    public String getPassword() {
        return password;
    }

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

}

在创建/注册用户期间,必须提供密码,但稍后在询问用户信息时,XML 不应包含密码元素。是否可以将元素定义为只写?

4

3 回答 3

6

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB 2 (JSR-222)专家组的成员。

MOXy 有一个@XmlReadOnly扩展名。带有注释的字段/属性@XmlReadOnly将在解组(读取)期间填充,但不会在编组期间写入。

用户

package forum10208143;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlReadOnly;

/**
 * @author Martin Burchard
 * 
 */
@XmlRootElement(name = "user")
public class User {
    private String id;
    private String nickname;
    private String email;
    private String password;

    @XmlElement(name = "id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlElement(name = "nickname")
    public String getNickName() {
        return nickname;
    }

    public void setNickName(String nickname) {
        this.nickname = nickname;
    }

    @XmlElement(name = "email")
    public String getEMail() {
        return email;
    }

    public void setEMail(String email) {
        this.email = email;
    }

    @XmlElement(name = "password")
    @XmlReadOnly
    public String getPassword() {
        return password;
    }

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

}

jaxb.properites

要将 MOXy 指定为您的 JAXB 提供程序,您需要添加一个jaxb.properties在与您的域模型相同的包中调用的文件,其中包含以下条目:

javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory

演示

package forum10208143;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(User.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum10208143/input.xml");
        User user = (User) unmarshaller.unmarshal(xml);

        System.out.println(user.getPassword());

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(user, System.out);
    }

}

输入.xml

<?xml version="1.0" encoding="UTF-8"?>
<user>
    <id>jdoe</id>
    <nickname>Jane</nickname>
    <email>jane.doe@example.com</email>
    <password>secret</password>
</user>

输出

secret
<?xml version="1.0" encoding="UTF-8"?>
<user>
   <email>jane.doe@example.com</email>
   <id>jdoe</id>
   <nickname>Jane</nickname>
</user>
于 2012-04-18T14:29:01.047 回答
1

据我所知,没有这样的选项,我建议您使用不包含密码字段的派生类,如“ReadOnlyUser”。

于 2012-04-18T11:41:13.070 回答
1

我是这样做的:

  • 我将访问类型设置为无,以便能够注释字段和属性。
  • 然后用返回* *的方法注释密码属性。
  • 然后是另一个返回未加密密码的 getter。

不知道这是否全部被接受,但到目前为止它有效....

@XmlRootElement(name = "User")
@XmlAccessorType(XmlAccessType.NONE)
public class User {

...

private String password = null;
@XmlElement(required=false, name="password")
public String getPassword() { 
    return (password == null || password.isEmpty()) ? null : "**********"; 
}

@XmlTransient
public String getUnencryptedPassword() { 
    return this.password; 
}
于 2012-08-13T17:53:18.967 回答