我正在使用JAXB和JSON实现jqGrid消费的通用java POJO包装器。这是一个CXF服务,所以我选择的marshaller库是Jettison或Jackson:
@XmlRootElement(name = "response")
public class JQGridResponseWrapper<T> {
private PaginatedResults<T> results;
public JQGridResponseWrapper() {
}
public JQGridResponseWrapper(PaginatedResults<T> results) {
this.results = results;
}
@XmlElementWrapper(name = "records")
@XmlElement(name = "record")
public List<T> getRecords() {
return results.getRecords();
}
@XmlElement(name = "pager")
public Pager getPager() {
return results.getPager();
}
}
这是一个由通用包装器包装的示例POJO:
@XmlRootElement
public class Note {
private Long id;
private String subject;
private String description;
private Project project;
public Note() {}
public Note(Long id, String subject, String description, Project project) {
this.id = id;
this.subject = subject;
this.description = description;
this.project = project;
}
@XmlElement(name="noteId")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
}
当编组到XML时,一切正常,所有类型都被正确映射,并且我得到一个包含<records>
元素数组的父<record>
元素。但是当编组到JSON(项目要求)时,'record'元素是未命名的,这使得jqGrid阻塞:
{"records":[
{"subject":"subject aaa",
"description":"Description dsifj ofdisjo",
"project":{
"projectCode":"HWIIA",
"description":"project description",
"brand":null,
"projectId":101
},
"noteId":201
},
{"subject":"subject bbb",
"description":"Description odisfj doisjf odsijf",
"project":{
"projectCode":"HWIIA",
"description":"project description",
"brand":null,
"projectId":101
},
"noteId":202
},
{"subject":"subject ccc",
"description":"Description oijgf gfoij jgifif",
"project":{
"projectCode":"HWIIA",
"description":"project description",
"brand":null,
"projectId":101
},
"noteId":203
}
],
"pager"{
"recordsPerPage":10,
"currentPage":1,
"fromRecord":1,
"toRecord":3,
"totalRecords":3,
"totalPages":1}}
我需要为记录数组中的每条记录命名。是否有一种简单的方法可以使用Jettion或Jackson来完成这项工作?我搜索并搜索了网络,但找不到我目标编组库的直接解决方案。我确实看到了MOXY的一些答案,但是在这一点上我改变库是有问题的。非常感谢任何帮助。