我正在使用带有MOXy的Apache Wink客户端对Microsoft Sharepoint进行RESTful Web服务调用。
我接近完成我的JSON / POJO映射,但我仍然坚持剩下3个元素:CreatedBy,ModifiedBy和Attachments。当我在调试器中检查Results对象时,所有3个对象的值都为null。
这是我想要映射的JSON响应:
{
"d": {
"results": [
{
"__metadata": {
"uri": "https://worksites.connect.somecompany.com/sites/UniversityRelations/_vti_bin/listdata.svc/UserInformationList(1)",
"etag": "W/\"63\"",
"type": "Microsoft.SharePoint.DataService.UserInformationListItem"
},
"ContentTypeID": "0x010A005977DE477030BD4EADA71E1A1B9F4069",
"Name": "Some name",
"Account": "Some account",
"WorkEMail": null,
"MobilePhone": null,
"AboutMe": null,
"SIPAddress": null,
"IsSiteAdmin": true,
"Deleted": false,
"Picture": null,
"Department": null,
"Title": null,
"FirstName": "Some first name",
"LastName": "Some last name",
"WorkPhone": null,
"UserName": "Some username",
"WebSite": null,
"AskMeAbout": null,
"Office": null,
"Id": 1,
"ContentType": "Person",
"Modified": "\/Date(1384765618000)\/",
"Created": "\/Date(1372051813000)\/",
"CreatedBy": {
"__deferred": {
"uri": "https://worksites.connect.somecompany.com/sites/UniversityRelations/_vti_bin/listdata.svc/UserInformationList(1)/CreatedBy"
}
},
"CreatedById": 1,
"ModifiedBy": {
"__deferred": {
"uri": "https://worksites.connect.somecompany.com/sites/UniversityRelations/_vti_bin/listdata.svc/UserInformationList(1)/ModifiedBy"
}
},
"ModifiedById": 1073741823,
"Owshiddenversion": 63,
"Version": "1.0",
"Attachments": {
"__deferred": {
"uri": "https://worksites.connect.somecompany.com/sites/UniversityRelations/_vti_bin/listdata.svc/UserInformationList(1)/Attachments"
}
},
"Path": "/sites/UniversityRelations/_catalogs/users"
}]
}
}
这是我的结果课:
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;
/**
*
* @author Chris Harris
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) <!-- Allows the __Metadata class to be mapped -->
public class Results {
private __Metadata __metadata;
@XmlAttribute(name="ContentTypeID")
private String contentTypeID;
@XmlAttribute(name="Name")
private String name;
@XmlAttribute(name="Account")
private String account;
@XmlAttribute(name="WorkEMail")
private String workEMail;
@XmlAttribute(name="MobilePhone")
private String mobilePhone;
@XmlAttribute(name="AboutMe")
private String aboutMe;
@XmlAttribute(name="SIPAddress")
private String sIPAddress;
@XmlAttribute(name="IsSiteAdmin")
private String isSiteAdmin;
@XmlAttribute(name="Deleted")
private String deleted;
@XmlAttribute(name="Picture")
private String picture;
@XmlAttribute(name="Department")
private String department;
@XmlAttribute(name="Title")
private String title;
@XmlAttribute(name="FirstName")
private String firstName;
@XmlAttribute(name="LastName")
private String lastName;
@XmlAttribute(name="WorkPhone")
private String workPhone;
@XmlAttribute(name="UserName")
private String userName;
@XmlAttribute(name="WebSite")
private String webSite;
@XmlAttribute(name="AskMeAbout")
private String askMeAbout;
@XmlAttribute(name="Office")
private String office;
@XmlAttribute(name="Id")
private String id;
@XmlAttribute(name="ContentType")
private String contentType;
@XmlAttribute(name="Modified")
private String modified;
@XmlAttribute(name="Created")
private String created;
@XmlAttribute(name="CreatedBy")
private CreatedBy createdBy;
@XmlAttribute(name="CreatedById")
private String createdById;
@XmlAttribute(name="ModifiedBy")
private ModifiedBy modifiedBy;
@XmlAttribute(name="ModifiedById")
private String modifiedById;
@XmlAttribute(name="Owshiddenversion")
private String owshiddenversion;
@XmlAttribute(name="Version")
private String version;
@XmlAttribute(name="Attachments")
private Attachments attachments;
@XmlAttribute(name="Path")
private String path;
}
由于CreatedBy,ModifiedBy和Attachments类目前的配置方式相同,这里只是我的CreatedBy类:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Chris Harris
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CreatedBy {
private __Deferred __deferred;
public __Deferred getDeferred() {
return __deferred;
}
public void setDeferred(__Deferred __deferred) {
this.__deferred = __deferred;
}
}
如何映射CreatedBy,ModifiedBy和附件?
答案 0 :(得分:1)
目前,Results
类上的所有内容都与@XmlAttribute
映射。 @XmlAttribute
只应用于简单属性(即String
,int
,byte[]
)或域对象,其中一个字段/属性与@XmlValue
映射。您应该将映射更改为:
@XmlElement(name="ModifiedBy")
private ModifiedBy modifiedBy;
除非您也将此对象模型编组为XML,否则我会将您的所有@XmlAttribute
映射更改为@XmlElement
。