我正在开发球衣网络服务。 像下面这样是我在我的球衣客户端收到的 json 字符串
{"license":"12345","dimRequirement":[{"alternateId":"AlternateID","classification":"1"}] }
但是当我解析时它给出了错误。我使用杰克逊进行解析
Unrecognized field "dimRequirement" (Class com.vxl.AppObject), not marked as ignorable
at [Source: java.io.StringReader@7be6f06c; line: 1, column: 49] (through reference chain: com.vxl.appanalytix.AppObject["dimRequirement"])
at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:244)
at org.codehaus.jackson.map.deser.StdDeserializer.reportUnknownProperty(StdDeserializer.java:589)
at org.codehaus.jackson.map.deser.StdDeserializer.handleUnknownProperty(StdDeserializer.java:575)
at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:684)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:515)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:351)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2131)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1402)
at com.vxl.CheckJersy.main(CheckJersy.java:56)
我尝试通过添加类型参考进行解析
new ObjectMapper().readValue(
output,new TypeReference<AppObject<DimRequirement>>() { } );
泛型类
@XmlRootElement
@XmlSeeAlso({ DimRequirement.class})
public class AppObject<T> implements Serializable {
private List<T> list;
private String license;
public AppObject() {
list = new ArrayList<T>();
}
public AppObject(List<T> list) {
this.list = list;
}
@XmlAnyElement(lax = true)
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public String getLicense() {
return license;
}
public void setLicense(String license) {
this.license = license;
}
}
服务
我想为多个返回的 POST 请求使用相同的服务 AppObject<xxx>,AppObject<yyy>
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/getreq")
@Override
public AppObject getAllByClientNIsCurrent(
AppObject<DimRequirement> appObject) {
List<DimRequirement> dimreqlist = dimRequirementDao
.getAllByClientNIsCurrent(appObject.getClientKey());
AppObject<DimRequirement> appObject = new AppObject<DimRequirement>();
appObject.setList(dimreqlist);
return appObject;
}