I develop a custom rest service for a custom gadget.
When calling this service, I get back in my gadget.xml a JSON result but some subdata are missing (ArrayList).
I only get first elements level.
I made the same code outside of Jira plugin/gadget (simple rest project) and it works fine.
My server side code :
@GET
@Path("/myservice")
@Produces({"application/json"})
public List<MyObject> getAll() {
List<MyObject> result = new ArrayList<MyObject>();
MyObject o = new MyObject();
o.setId(1);
o.setName("Name 1");
o.addSubObject(new MySubObject("1.1"));
o.addSubObject(new MySubObject("1.2"));
result.add(o);
return result;
}
@XmlRootElement
public class MyObject {
private Integer id;
private String name;
private List<MySubObject> subs;
public MyObject() {
this.subs = new ArrayList<MySubObject>();
}
@XmlElement(name="id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@XmlElement(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElementWrapper(name="subs")
@XmlElement(name="sub")
public List<MySubObject> getSubs() {
return subs;
}
public void addSubObject(MySubObject subO) {
this.subs.add(subO);
}
}
@XmlRootElement
public class MySubObject {
private String subName;
public MySubObject (String subName) {
this.subName = subName;
}
@XmlElement(name="subname")
public String getSubName() {
return subName;
}
}
My client side code :
<script type="text/javascript">
(function () {
AJS.Gadget({
baseUrl: "__ATLASSIAN_BASE_URL__",
useOauth: "/rest/gadget/1.0/currentUser",
config: {...},
view: {
template: function(args) {
console.log(args);
/**
MUST RETURN:
{"id":"1", "name":"Name 1", "subs":[{"subname":"1.1"}, {"subname":"1.2"]}
BUT GET:
{"id":"1", "name":"Name 1"}
*/
},
args: [{
key: "users",
ajaxOptions: function() {
return {
url: "/rest/myservice/latest/myservice.json"
};
}
}]
}
});
})();
</script>
Community moderators have prevented the ability to post new answers.
2 cents: try to use:
a) First, change the getter to return an array []
b) Then, try to use this style:
(new GenericEntity<List<MyObject>>(obj){}) & correctly return the response, like the following:
public Response getSomething() {
return Response.ok(new GenericEntity<List<MyObject>>(createListHere()){}).build();
//(Create generic wrapper for MySubObject too)
}
Hi Radu,
Those two solutions have the same effect : incomplete JSON result : [{"id":"1", "name":"Name 1"}].
To be complete in my problem, the same genereted response by using xml format is ok as there are sub objects... Any Idea, is there the version of jersey or anything else ?
Thanks for your answer.
Regards,
Johann.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Well, first obs: I suppose you add it :), not like in your example, where:
List<MyObject> result = new ArrayList<MyObject>();
MyObject o = new MyObject();
/* fill in object, but where's result.add() ? */
return result;\
What I believe, looking more closely, it should be linked by: @XmlElementWrapper (), @XmlElement(name="sub") then again @XmlElement(name="subname"). If I remember well, these 2 should match, or the first one should be removed ?!?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes you're write, I forget to write result.add(o) in my example but not in my real code :)
Yes the first one could be removed but not working so far...
To not be blocked, I manually write my json result ;)
Keep in touch.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.