user2961454 user2961454 - 1 year ago 37
reST (reStructuredText) Question

Can not parse JSON in REST webservices

I have developed web service that can parse XML payload using JAXB framework. Now I need to extend this web service to parse the same payload in JSON format. The below logic that works for XML payload doesn't work for JSON payload. Appreciate your inputs.

XML Payload

<?xml version="1.0" encoding="UTF-8" ?>
<LinkUserToGroup>
<groups>
<group>
<id>dbb8</id>
</group>
<group>
<id>ce50</id>
</group>
</groups>
</LinkUserToGroup>


JSON Payload(which doesn't work)

{
"LinkUserToGroup": {
"groups": {
"group": [
{ "id": "dbb8" },
{ "id": "ce50" }
]
}
}
}


POJO Classes

LinkUserToGroup.java

@XmlRootElement(name = "LinkUserToGroup")
@XmlAccessorType (XmlAccessType.FIELD)
@JsonIgnoreProperties(ignoreUnknown = true)
public class LinkUserToGroup {

@JsonProperty("groups")
@XmlElement(name = "groups")
private List<Groups> groupsList;

public List<Groups> getGroupsList() {
return groupsList;
}

public void setGroupsList(List<Groups> groupsList) {
this.groupsList = groupsList;
}
}


Groups.java

@XmlRootElement(name = "groups")
@XmlAccessorType (XmlAccessType.FIELD)
public class Groups {

@XmlElement(name = "group")
@JsonProperty("group")
private List<GroupVO> groups;

public List<GroupVO> getGroups() {
return groups;
}

public void setGroups(List<GroupVO> groups) {
this.groups = groups;
}

}


GroupVO.java

@XmlRootElement(name = "group")
@XmlAccessorType (XmlAccessType.FIELD)
public class GroupVO {

@JsonProperty("id")
@XmlElement(name = "id")
private String id;

public String getID() {
return id;
}

public void setURI(String id) {
this.id = id;
}
}


service.java

@POST
@Path("/{id}/groups")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public Response addGroupToUser(@PathParam("id") String key, LinkUserToGroup grpList) {

List<Groups> groups = grpList.getGroupsList();
List<GroupVO> groupVO;
for (Groups gr : groups) {
groupVO = gr.getGroups();
for(GroupVO g : groupVO){
eachGroupID = g.getID();
logger.debug(" Current Group is::"+eachGroupID);
}
}
...
...
}


I am getting null pointer exception at for (Groups gr : groups)

Stack Trace

May 20, 2017 10:43:42 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at com.services.UserGroupWSOperations.addGroupToUser(UserGroupWSOperations.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)


Java Build Path for this project is below

enter image description here

Answer Source

There is no grouplist available in the JSON data that you send that's why grpList.getGroupsList() returns null. From the POJO definition of LinkUserToGroup, the key groups should be a JSON array. Please try with the following JSON data.

{
   "groups": [
      {
         "group": [
            {
               "id": "dbb8"
            },
            {
               "id": "ce50"
            }
         ]
      }
   ]
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download