我在 google-http-java-client 中使用 Jackson 2 作为解析器。当一个数字映射到 String 类的字段时,我得到一个IllegalArgumentException
. 在这种情况下,整数1234
即将被映射到Model.b
一个字符串。但是,一个字符串可以映射到一个整数字段,它"5678"
是Model.c
一个int。
Caused by: java.lang.IllegalArgumentException: expected numeric type but got class java.lang.String
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:843)
... 22 more
JSON是这样的:
{
"a": "Something",
"b": 1234,
"c": "5678"
}
POJO是这样的:
public class Model {
@Key String a;
@Key String b;
@Key int c;
}
如何解决?或者杰克逊可以通过定制解析来实现这一点吗?
更新
我在带有 RoboSpice 库的 Android 中使用它。这是gradle配置:
...
compile 'com.octo.android.robospice:robospice-google-http-client:1.4.14'
compile('com.google.http-client:google-http-client-jackson2:1.19.0') {
exclude module: 'xpp3'
exclude group: 'stax'
}
...
这是的输出./gradlew dependencies
:
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for de
bug as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class
packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for re
lease as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class
packages
WARNING [Project: :app] variant.getProcessResources() is deprecated. Call it on
one of variant.getOutputs() instead.
WARNING [Project: :app] variant.getProcessResources() is deprecated. Call it on
one of variant.getOutputs() instead.
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
No configurations
BUILD SUCCESSFUL
Total time: 13.873 secs