[英]How to set root element as List in XML JAVA
I need a way to create the below object in java spring-boot controller.我需要一种在 java spring-boot 控制器中创建以下对象的方法。 it like
List<Object>
它就像
List<Object>
<?xml version="1.0" encoding="UTF-8"?>
<oauth-revocation>
<token type="access">AAETb2F1dGgtcmV2b2tlLWN1c3RvbfZaRlVbnPSc1...</token>
<token type="refresh">fZaRlVbnPSc1UGTjCRdq4mPbOosD2+aZIKbJ6bTeW...</token>
<resource-owner client-id="760d75a2-44b1-4485-8c6f-0d264fcf7398">laura</resource-owner>
<resource-owner client-id="760d75a2-44b1-4485-8c6f-0d264fcf7398">emily</resource-owner>
</oauth-revocation>
Issue is i don't know way to set root element as oauth-revocation for the list.问题是我不知道如何将根元素设置为列表的 oauth-revocation。
Implement that as a POJO with 2 List
fields, not as a List<Object>
:将其实现为具有 2 个
List
字段的 POJO,而不是List<Object>
:
@XmlRootElement(name = "oauth-revocation")
public class OauthRevocation {
@XmlElement(name = "token")
private List<Token> tokens;
@XmlElement(name = "resource-owner")
private List<ResourceOwner> resourceOwners;
}
public class Token {
@XmlAttribute(name = "type")
private String type;
@XmlValue
private String value;
}
public class ResourceOwner {
@XmlAttribute(name = "client-id")
private String clientId;
@XmlValue
private String name;
}
Add constructors and other standard methods as needed.根据需要添加构造函数和其他标准方法。
Test测试
OauthRevocation root = new OauthRevocation(Arrays.asList(
new Token("access", "AAETb2F1dGgtcmV2b2tlLWN1c3RvbfZaRlVbnPSc1..."),
new Token("refresh", "fZaRlVbnPSc1UGTjCRdq4mPbOosD2+aZIKbJ6bTeW...")
), Arrays.asList(
new ResourceOwner("760d75a2-44b1-4485-8c6f-0d264fcf7398", "laura"),
new ResourceOwner("760d75a2-44b1-4485-8c6f-0d264fcf7398", "emily")
));
Marshaller marshaller = JAXBContext.newInstance(OauthRevocation.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
Output输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<oauth-revocation>
<token type="access">AAETb2F1dGgtcmV2b2tlLWN1c3RvbfZaRlVbnPSc1...</token>
<token type="refresh">fZaRlVbnPSc1UGTjCRdq4mPbOosD2+aZIKbJ6bTeW...</token>
<resource-owner client-id="760d75a2-44b1-4485-8c6f-0d264fcf7398">laura</resource-owner>
<resource-owner client-id="760d75a2-44b1-4485-8c6f-0d264fcf7398">emily</resource-owner>
</oauth-revocation>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.