我是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不应包含密码元素。 是否可以将Element定义为writeonly?
答案 0 :(得分:6)
注意:我是EclipseLink JAXB (MOXy)主管,是JAXB 2 (JSR-222)专家组的成员。
MOXy的扩展名为@XmlReadOnly
。注释为@XmlReadOnly
的字段/属性将在unmarshal(读取)期间填充,但不会在编组期间写入。
用户强>
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);
}
}
<强> input.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>
答案 1 :(得分:1)
据我所知,没有这样的选项,我建议您使用类似“ReadOnlyUser”的派生类,不包括密码字段。
答案 2 :(得分:1)
我就这样做了:
不知道它是否全部被接受,但到目前为止它都有效......
@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;
}