我正在使用 JAXB 和 JSON 为 jqGrid 消费实现一个通用的 java POJO 包装器。这是一个 CXF 服务,所以我选择的编组库是 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(项目要求)时,“记录”元素是未命名的,这使得 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 的一些答案,但此时更改库对我来说是有问题的。非常感谢任何帮助。