chi*_*ile 19 java json jackson
我收到以下错误,我发现没有解决方法对我有所帮助:
无法识别的字段"GaugeDeviceId"(Class GaugeDevice),未标记为可忽略
问题似乎是,服务返回带有大写字母的属性名称,而类属性以较低的字母开头.
我试过了:
@JsonProperty("SerialNo")到属性实例化 - 相同的错误@JsonProperty("SerialNo")到相应的getter - 相同的错误@JsonProperty("SerialNo")到相应的setter - 相同的错误@JsonProperty("SerialNo")到所有这些(只是为了好玩) - 同样的错误(注意:@JsonProperty("SerialNo")只是一个例子)
奇怪的是,那个注释:@JsonIgnoreProperties(ignoreUnknown = true)应该完全抑制那个错误,但它仍在触发......
这里的课程:(注意:不完整)
@JsonIgnoreProperties(ignoreUnknown = true)
public class GaugeDevice
{
private int gaugeDeviceId;
private Date utcInstallation;
private String manufacturer;
private float valueOffset;
private String serialNo;
private String comment;
private int digitCount;
private int decimalPlaces;
@JsonProperty("SerialNo")
public String getSerialNo() {
return serialNo;
}
public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}
@JsonProperty("Comment")
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
Run Code Online (Sandbox Code Playgroud)
这里的出路在哪里?请帮忙.
编辑:
这是Client Class :(只是一个简单的测试客户端)
import ccc.android.meterdata.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import org.glassfish.jersey.jackson.JacksonFeature;
public class RestClient
{
private String connectionUrl;
private javax.ws.rs.client.Client client;
public RestClient(String baseUrl) {
client = ClientBuilder.newClient();;
connectionUrl = baseUrl;
client.register(JacksonFeature.class);
}
public GaugeDevice GetGaugeDevice(int id){
String uri = connectionUrl + "/GetGaugeDevice/" + id;
Invocation.Builder bldr = client.target(uri).request("application/json");
return bldr.get(GaugeDevice.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望错误有根源吗?
Chr*_*tos 17
我遇到了同样的问题,我通过更改注释导入来解决它:
com.fasterxml.jackson.annotation.JsonIgnoreProperties
Run Code Online (Sandbox Code Playgroud)
至
org.codehaus.jackson.annotate.JsonIgnoreProperties
Run Code Online (Sandbox Code Playgroud)
没有定义任何NamingStrategy或ObjectMapper.
小智 10
我遇到了同样的问题,并通过更改注释导入来解决它
com.fasterxml.jackson.annotation.JsonProperty
Run Code Online (Sandbox Code Playgroud)
至
org.codehaus.jackson.annotate.JsonProperty
Run Code Online (Sandbox Code Playgroud)
鉴于以下是您的错误:
无法识别的字段"GaugeDeviceId"(Class GaugeDevice),未标记为可忽略
我很确定你需要为GaugeDeviceId物业做同样的事情,就像你为SerialNo物业做的那样.
@JsonProperty("SerialNo")
public String getSerialNo() {
return this.serialNo;
}
@JsonProperty("GaugeDeviceId")
public int getGaugeDeviceId() {
return this.gaugeDeviceId;
}
Run Code Online (Sandbox Code Playgroud)
在这里,我有一个快速测试类,不会抛出错误.
import org.codehaus.jackson.map.ObjectMapper;
public class JsonDeserialization {
public static void main(final String[] args) {
final String json = "{ \"SerialNo\":\"123\", \"GaugeDeviceId\":\"456\"}";
final ObjectMapper mapper = new ObjectMapper();
try {
final GaugeDevice readValue = mapper.readValue(json, GaugeDevice.class);
System.out.println(readValue.getSerialNo());
System.out.println(readValue.getGaugeDeviceId());
} catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
它输出:
123
456
Run Code Online (Sandbox Code Playgroud)
编辑:版本信息
并不重要,因为我相信以上都是使用jackson的一些非常标准的东西,我使用的是核心asl的1.9.13版本和mapper-asl库
编辑:客户提供
我想知道这是否与这个问题有关?我相信解决方案是您正在使用的依赖项的配置.
我不确定,但我觉得我接近以下依赖设置(注意我正在使用maven)
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>2.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
另外要检查的是PropertyNamingStrategy,这将允许Jackson使用"Pascal命名"并将JSON属性与POJO属性相匹配.请参见f.ex:http://www.javacodegeeks.com/2013/04/how-to-use-propertynamingstrategy-in-jackson.html
在类级别使用注释进行反序列化时,可以忽略未知属性.
例如 :
@JsonIgnoreProperties(ignoreUnknown=true)
class Foo{
...
}
Run Code Online (Sandbox Code Playgroud)
上面的代码段将忽略任何未知属性.(注释导入:org.codehaus.jackson.annotate.JsonIgnoreProperties)
| 归档时间: |
|
| 查看次数: |
82367 次 |
| 最近记录: |