我有一个对象,我从XML格式的REST API返回。我可以创建和更新此对象类型。在我的Rest客户端中,我想提供一些带有我的元素的属性。让我们说我可以指定一个属性,比如access =" LOCKED"在用户界面中将该字段设置为只读。
示例:用户对象
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "User")
public class User {
private String userName;
private String userId;
private String firstName;
private String lastName;
private String email;
@XmlAttribute(name="username")
public void setUserName(String userName) {
this.userName = userName;
}
@XmlAttribute(name = "userId", namespace = "some namespace")
public void setUserId(String userId) {
this.userId = userId;
}
@XmlElement(name = "FirstName")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@XmlElement(name = "LastName")
public void setLastName(String lastName) {
this.lastName = lastName;
}
@XmlElement(name = "Email")
public void setEmail(String email) {
this.email = email;
}
}
使用JAXB进行封送:
User user = User.builder().email("e@mail.com").userName("jd")
.userId("123").firstName("Jane").lastName("Doe").build();
JAXBContext jaxbContext = JAXBContext.newInstance(Iser.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(user, System.out);
假设我想设置Email和LastName的访问权限,你会如何注释这个" access"将它放在每个元素上的属性?
具有访问属性的预期输出:
<User username="jd" ns:userId="123">
<FisrtName>Jane</FirstName>
<LastName access="LOCKED">Doe</LastName>
<Email access="LOCKED">e@mail.com</Email>
</User>
答案 0 :(得分:0)
试试这个
示例:用户对象
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement(name = "User")
public class User {
private String userName;
private String userId;
private String firstName;
private LastName lastName;
private Email email;
@XmlAttribute(name="username")
public void setUserName(String userName) {
this.userName = userName;
}
@XmlAttribute(name = "userId", namespace = "some namespace")
public void setUserId(String userId) {
this.userId = userId;
}
@XmlElement(name = "FirstName")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@XmlRootElement(name = "LastName")
@XmlAccessorType (XmlAccessType.FIELD)
public static class LastName{
@XmlValue
public String lastnme;
//put getters and setters
@XmlAttribute(name ="access")
public String access;
//put getters and setters
}
@XmlRootElement(name = "Email")
@XmlAccessorType (XmlAccessType.FIELD)
public static class Email{
@XmlValue
public String Email_id;
//put getters and setters
@XmlAttribute(name ="access")
public String access;
//put getters and setters
}
}
使用JAXB进行封送:
User.Lastname lastname = new User.Lastname();
lastname.setlastnme("Doe");
lastname.setAccess("LOCKED");
user.Email email_id = new user.Email();
email_id.setEmail_Id("e@mail.com");
email_id.setAccess("LOCKED");
User user = User.builder().email(email_id).userName("jd")
.userId("123").firstName("Jane").lastName(lastname).build();
JAXBContext jaxbContext = JAXBContext.newInstance(Iser.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(user, System.out);