[英]Unrecognized field , not marked as ignorable ,when parsing json string
I am working on a Jersey rest services. 我正在从事泽西岛的休息服务。 trying to pass a generic object(in my case AppObject) as post request and the response from the server is also the generic object(in my case AppObject) i am expecting a string {"license":"12345","list":[{"alternateId":"AlternateID","classification":"1"}] }
but insted of list i am getting dimRequirement as shown below. 尝试将通用对象(在我的情况下为AppObject)作为后期请求传递,并且服务器的响应也是通用对象(在我的情况下为AppObject),我期望一个字符串{"license":"12345","list":[{"alternateId":"AlternateID","classification":"1"}] }
但由于列表的原因,我得到了dimRequirement ,如下所示。 So when parsing the json am getting the exception. 因此,在解析json时会遇到异常。 Is there a way to get it as list itself with out changing the code. 有没有一种方法可以将其作为列表本身而无需更改代码。 Or any alternative could anybody help me. 或任何其他方式可以帮助我。
Json string receiving at the client 客户端接收Json字符串
{"license":"12345","dimRequirement":[{"alternateId":"AlternateID","classification":"1"}] }
my client 我的客户
AppObject<DimRequirement> appObject = new AppObject<DimRequirement>();
appObject.setClientKey(4L);
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/myproject/");
JSONObject json = new JSONObject(appObject);
ClientResponse response = webResource.path("rest").path("requirement/getreq").type("application/json").accept("application/json")
.post(ClientResponse.class,json.toString());
String output = response.getEntity(String.class);
System.out.println(output);
appObject= new ObjectMapper().readValue(
output, AppObject.class);
error 错误
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)
Generic Class 通用类
@XmlRootElement
@XmlSeeAlso({DimRequirement.class })
public class AppObject<T> implements Serializable {
private List<T> list;
private Long clientKey;
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 Long getClientKey() {
return clientKey;
}
public void setClientKey(Long clientKey) {
this.clientKey = clientKey;
}
}
service 服务
@Path("/requirement")
public class DimRequirementManagerImpl {
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/getreq")
@Override
public AppObject getAllByClientNIsCurrent(
AppObject<DimRequirement> appObj) {
List<DimRequirement> dimreqlist = dimRequirementDao
.getAllByClientNIsCurrent(appObj.getClientKey());
AppObject appObject = new AppObject();
appObject.setList(dimreqlist);
return appObject;
}}
DimRequirement which is setting to AppObject 设置为AppObject的DimRequirement
@XmlRootElement
public class DimRequirement extends BaseObject implements Serializable {
private Long requirementKey;
private String description;
private String priority;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="requirementKey")
public Long getRequirementKey() {
return this.requirementKey;
}
public void setRequirementKey(Long requirementKey) {
this.requirementKey = requirementKey;
}
@Column(name="Description")
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name="Priority")
public String getPriority() {
return this.priority;
}
public void setPriority(String priority) {
this.priority = priority;
}
}
i have changed the return type of service to string(json string). 我已将服务的返回类型更改为string(json string)。
service 服务
@Path("/requirement")
public class DimRequirementManagerImpl {
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/getreq")
@Override
public String getAllByClientNIsCurrent(AppObject<DimRequirement> appObj) {
try {
List<DimRequirement> dimreqlist = dimRequirementDao.getAllByClientNIsCurrent(appObj.getClientKey());
AppObject appObject = new AppObject();
appObject.setList(dimreqlist);
JSONObject jsonget = new JSONObject(appObject);
return jsonget.toString();
}catch (Exception e) {
AppObject<DimRequirement> appObject = new AppObject<DimRequirement>();
appObject.setLicense("12345");
JSONObject jsonget = new JSONObject(appObject);
return jsonget.toString();
}
}}
this worked for the same client in the question returnig json {"license":"12345","list":[{"alternateId":"AlternateID","classification":"1"}] } 这对问题returnig json {“ license”:“ 12345”,“ list”:[{“ alternateId”:“ AlternateID”,“ classification”:“ 1”}]}的同一客户有效
client 客户
AppObject<DimRequirement> appObject = new AppObject<DimRequirement>();
appObject.setClientKey(4L);
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/myproject/");
JSONObject json = new JSONObject(appObject);
ClientResponse response = webResource.path("rest").path("requirement/getreq").type("application/json").accept("application/json")
.post(ClientResponse.class,json.toString());
String output = response.getEntity(String.class);
AppObject<DimRequirement> appObjectclientns = new ObjectMapper()
.readValue(output,
new TypeReference<AppObject<DimRequirement>>() {
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.