问题描述
我收到以下错误,我发现没有解决方法对我有用:
I'm getting the following error and no resolution i found did the trick for me:
问题似乎是服务返回带有大写字母的属性名称,而类属性以较低的字母开头。
The problem seems, that the service returns the property names with a leading upper letter, while the class properties begin with a lower letter.
我试过:
- 将propertyNames更改为第一个大写字母 - 相同的错误
- 将
@JsonProperty(SerialNo)
添加到属性实例化中 - 相同错误 - 将
@JsonProperty(SerialNo)
添加到相应的getter中 - 同样的错误 - 将
@JsonProperty(SerialNo)
添加到相应的设置者 - 同样的错误 - 添加
@JsonProperty( SerialNo)
给所有人(只是为了好玩) - 同样的错误
- changing the propertyNames to first upper letter - same error
- adding
@JsonProperty("SerialNo")
to the property instantiation - same error - adding
@JsonProperty("SerialNo")
to the corresponding getters - same error - adding
@JsonProperty("SerialNo")
to the corresponding setters - same error - adding
@JsonProperty("SerialNo")
to all of them (just for fun) - same error
(注意: @JsonProperty( 的SerialNo)
只是一个例子)
(note: @JsonProperty("SerialNo")
is just an example)
奇怪的是,那个注释: @JsonIgnoreProperties(ignoreUnknown = true)
应该压制恰好是那个错误,但它仍在触发...
The strange thing is, that annotation: @JsonIgnoreProperties(ignoreUnknown = true)
should suppress exactly that error, but it is still triggering...
这里的类:(注意:不完整)
here the Class: (note: not complete)
@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;
}
这里的出路在哪里?请帮忙。
Where is the way out here? Please help.
编辑:
以下是客户类:(只是一个简单的测试客户端)
Here is the Client Class: (just a simple test client)
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);
}
}
我希望错误有根源吗?
推荐答案
另外要检查的是 PropertyNamingStrategy
,这将允许Jackson使用Pascal命名并将JSON属性与POJO属性相匹配。请参见此处的f.ex:
Another thing to check out is PropertyNamingStrategy
, which would allow Jackson to use "Pascal naming" and match JSON properties with POJO properties. See f.ex here: http://www.javacodegeeks.com/2013/04/how-to-use-propertynamingstrategy-in-jackson.html
这篇关于Jackson Json反序列化:未识别的字段“......” ,没有标记为可忽略的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!