0

I am implementing a generic java POJO wrapper for jqGrid consumption, using JAXB and JSON. This is a CXF service so my marshaller libraries of choice are either Jettison or 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();
}
}

Here's a sample POJO to be wraped by the generic wrapper:

@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;
}

}

When marshaling to XML, everything works fine, all types are correctly mapped, and I get a parent <records> element containing an array of <record> elements. But when marshaling to JSON (the project requirement), the 'record' element is unnamed, which makes jqGrid choke:

{"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}}

I need to get a name for each record in the records array. Is there a simple way to make this work, either with Jettion or Jackson? I searched and searched the web but couldn't find a straighforward solution for my target marshaler libraries. I did see some answers for MOXY, but it is problematic for me to change libraries at this point. Any help greatly appreciated.

Pablo
  • 167
  • 1
  • 2
  • 13
  • If you can't find what you need with Jettison or Jackson I would be happy to help you with MOXy, – bdoughan Jun 19 '13 at 14:30
  • Does MOXy work well with JAXB / CXF / JSON? – Pablo Jun 19 '13 at 16:44
  • MOXy is a JAXB (JSR-222) compliant implementation (I am a member of the expert group). MOXy is also the default JSON-binding provider in GlassFish 4 (see: http://blog.bdoughan.com/2013/05/eclipselink-25-release-available-for.html). With CXF you can use MOXyJsonProvider to enable MOXy as the JSON-binding provider: http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html – bdoughan Jun 19 '13 at 17:16
  • Thank you Blaise. I was hoping for a straightforward integration into CXF configuration, so I tried to add the MOXy provider this way: , which resulted in: "java.lang.ClassNotFoundException: org.eclipse.persistence.internal.queries.ContainerPolicy". Before I go and chase all dependencies .. can this approach work? – Pablo Jun 19 '13 at 21:16
  • Could you post an example of the JSON you are trying to produce? – bdoughan Jun 20 '13 at 17:48
  • 1
    In the end, I came to the conclusion that using JAXB with JSON is a bad idea. Since I only need JSON and not XML, I dropped JAXB altogether, and then everything just worked. JSON is much more straightforward then XML. It doesn't care for types or list element names. I'm using CXF with Jackson, and the occasional @JsonProperty annotation when I want to override a POJO property name. That's it. – Pablo Jun 24 '13 at 13:34

0 Answers0