我开发了可以使用JAXB框架解析XML有效负载的Web服务。现在我需要扩展此Web服务以解析JSON格式的相同有效负载。适用于XML有效负载的以下逻辑不适用于JSON有效负载。感谢您的意见。
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(不起作用)
{
"LinkUserToGroup": {
"groups": {
"group": [
{ "id": "dbb8" },
{ "id": "ce50" }
]
}
}
}
POJO课程
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);
}
}
...
...
}
我在 处获取空指针异常(组gr:组)
堆栈跟踪
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位于
之下答案 0 :(得分:1)
您发送的JSON数据中没有可用的组列表,这就是int[] a1 = new int[]{1,3,5};
int[] a2 = new int[]{2,4,6};
List<Integer> result = IntStream.concat(Arrays.stream(a1), Arrays.stream(a2))
.boxed()
.sorted()
.collect(Collectors.toList());
System.out.println(result);
返回grpList.getGroupsList()
的原因。
根据{{1}}的POJO定义,键null
应该是JSON数组。请尝试使用以下JSON数据。
LinkUserToGroup