[英]Jackson ObjectMapper Conflicting setter definitions (Android.Graphics.Bitmap)
I have this method : 我有这个方法:
public static Response deserializeResponse(String jsonResponse) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
try{
return mapper.readValue(jsonResponse, Response.class);
}catch(JsonParseException e){
e.printStackTrace();
}catch(JsonMappingException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return null;
}
Here is the class Response : 这是类Response:
public static class Response {
public String RequestType;
public Data data;
public void SetRequestType(String requestType) { this.RequestType = requestType; }
@JsonIgnore
@JsonProperty("data")
public void SetData(Data data) { this.data = data; }
public Response(){
data = new Data();
}
public static class Data {
public String Status;
public String ErrorCode;
public int versionCode;
public String Expiry;
public String ProductSKU;
public String Email;
public String Birthday;
public String PictureUrl;
public String Pin;
public List<PhoneNumber> Numbers = new ArrayList<PhoneNumber>();
public int IdTransaction;
public int SubProductId;
public List<Performer> Performers = new ArrayList<Performer>();
public Client Client;
public Data() {}
}
}
Here is the call from an other class : 这是来自其他班级的电话:
Response rep = JsonOperation.deserializeResponse(jsonString);
I run this code on a device that uses android 2.3.6 and everything works perfect. 我在使用android 2.3.6的设备上运行此代码,一切都很完美。 When i run this code on my Galaxy Note 3 or S4, I have severals errors.
当我在我的Galaxy Note 3或S4上运行此代码时,我有几个错误。 Here is the log :
这是日志:
W/System.err﹕ com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "imagePath": android.graphics.Bitmap#setImagePath(1 params) vs android.graphics.Bitmap#setImagePath(1 params)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:269)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:143)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:325)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:633)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:427)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:295)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:143)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:325)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:147)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:23)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:329)
03-21 15:51:54.373 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:633)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:427)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:295)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:143)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:325)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:633)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:427)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:295)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:143)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:342)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:2895)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2789)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1943)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.francovoyance.JsonOperation.deserializeResponse(JsonOperation.java:321)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.francovoyance.HttpOperation$HttpPostRequest.doInBackground(HttpOperation.java:125)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.francovoyance.HttpOperation$HttpPostRequest.doInBackground(HttpOperation.java:69)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "imagePath": android.graphics.Bitmap#setImagePath(1 params) vs android.graphics.Bitmap#setImagePath(1 params)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:196)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.filterBeanProps(BeanDeserializerFactory.java:614)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:522)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:267)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:171)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:388)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
03-21 15:51:54.383 15204-15218/com.francovoyance W/System.err﹕ ... 36 more
Here is the Json input from the server : 这是服务器的Json输入:
private string CheckVersion(JObject jsonObject)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
writer.WriteStartObject();
writer.WritePropertyName("RequestType");
writer.WriteValue("CheckVersion");
writer.WritePropertyName("data");
writer.WriteStartObject();
writer.WritePropertyName("Status");
string inVersion = (string)jsonObject["data"]["versionCode"];
string actualVersion = WebTV.Common.AppSettings.GetString("APP_VERSION", "1");
actualVersion = Convert.ToString((int)Convert.ToDouble(actualVersion));
if (inVersion.Equals(actualVersion))
{
writer.WriteValue("Ok");
}
else
{
writer.WriteValue("Error");
writer.WritePropertyName("versionCode");
writer.WriteRawValue(actualVersion);
}
writer.WriteEndObject();
}
return sb.ToString();
}
I searched and tried to find answers to this problem, but i didn't find anything about the setters in android.graphics.Bitmap... I tried to use @JsonIgnore without any results. 我搜索并试图找到这个问题的答案,但我没有找到任何关于android.graphics.Bitmap中的setter ...我试图使用@JsonIgnore没有任何结果。 I need help to resolve the exception when I'm trying to use the mapper.
当我尝试使用映射器时,我需要帮助来解决异常。 Any help would be appreciated.
任何帮助,将不胜感激。
I just solved this issue (for me), though it feels like more of a workaround for these Samsung devices. 我刚刚解决了这个问题(对我来说),虽然这对于这些三星设备来说更像是一种解决方法。 I realize you mentioned you tried the @JsonIgnore annotation, but it managed to solve my issue.
我意识到你提到你尝试了@JsonIgnore注释,但它设法解决了我的问题。
In my model, I was only mapping the JSON to a small subset of my base model. 在我的模型中,我只是将JSON映射到我的基本模型的一小部分。 In this base model however, I had a List of another Object model that contained a Bitmap.
但是,在这个基本模型中,我有一个包含Bitmap的另一个Object模型的List。 By adding @JsonIgnore to the List of models, the issue was resolved (although I wasn't mapping to them and they should have been ignored).
通过将@JsonIgnore添加到模型列表中,问题得到了解决(尽管我没有映射到它们,它们应该被忽略)。
I had been using an ObjectMapper feature to ignore unused values, but it did not work on the Samsung devices. 我一直在使用ObjectMapper功能来忽略未使用的值,但它在三星设备上不起作用。
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
In the example below, my Profile object contains a Bitmap, and I'm only mapping the name and age with the JSON. 在下面的示例中,我的Profile对象包含一个Bitmap,我只用JSON映射名称和年龄。
Example: 例:
public class A {
private String name;
private int age;
@JsonIgnore
private List<Profile> profiles;
}
If I had to make a wild guess, I'd say your Performer object above contains a Bitmap, and if you're not mapping to that, then I wouldd try and ignore the List. 如果我不得不猜测,我会说上面的Performer对象包含一个Bitmap,如果你没有映射到那个,那么我会尝试忽略List。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.