Java Jackson Json 反序列化:无法识别的字段 "..." ,未标记为可忽略

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22173418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:04:00  来源:igfitidea点击:

Hymanson Json Deserialisation: Unrecognized field "..." , not marked as ignorable

javajsonHymanson

提问by chile

I'm getting the following error and no resolution i found did the trick for me:

我收到以下错误,我发现没有解决方案对我有用:

Unrecognized field "GaugeDeviceId" (Class GaugeDevice), not marked as ignorable

无法识别的字段“GaugeDeviceId”(类 GaugeDevice),未标记为可忽略

The problem seems, that the service returns the property names with a leading upper letter, while the class properties begin with a lower letter.

问题似乎是,服务返回带有前导大写字母的属性名称,而类属性以小写字母开头。

I tried:

我试过:

  1. changing the propertyNames to first upper letter - same error
  2. adding @JsonProperty("SerialNo")to the property instantiation - same error
  3. adding @JsonProperty("SerialNo")to the corresponding getters - same error
  4. adding @JsonProperty("SerialNo")to the corresponding setters - same error
  5. adding @JsonProperty("SerialNo")to all of them (just for fun) - same error
  1. 将 propertyNames 更改为第一个大写字母 - 同样的错误
  2. 添加@JsonProperty("SerialNo")到属性实例化 - 同样的错误
  3. 添加@JsonProperty("SerialNo")到相应的吸气剂 - 同样的错误
  4. 添加@JsonProperty("SerialNo")到相应的设置器 - 同样的错误
  5. 添加@JsonProperty("SerialNo")到所有这些(只是为了好玩) - 同样的错误

(note: @JsonProperty("SerialNo")is just an example)

(注:@JsonProperty("SerialNo")只是一个例子)

The strange thing is, that annotation: @JsonIgnoreProperties(ignoreUnknown = true)should suppress exactly that error, but it is still triggering...

奇怪的是,那个注释:@JsonIgnoreProperties(ignoreUnknown = true)应该完全抑制那个错误,但它仍然在触发......

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.

这里的出路在哪里?请帮忙。

edit:

编辑:

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.Hymanson.HymansonFeature;

public class RestClient
{
    private String connectionUrl;
    private javax.ws.rs.client.Client client;

    public RestClient(String baseUrl) {
         client = ClientBuilder.newClient();;
         connectionUrl = baseUrl;
         client.register(HymansonFeature.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);
    }
}

I hope the error has its root here?

我希望错误的根源在这里?

采纳答案by StaxMan

Another thing to check out is PropertyNamingStrategy, which would allow Hymanson 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-Hymanson.html

另一件要检查的事情是PropertyNamingStrategy,这将允许 Hymanson 使用“Pascal 命名”并将 JSON 属性与 POJO 属性匹配。请参阅此处的 f.ex:http: //www.javacodegeeks.com/2013/04/how-to-use-propertynamingstrategy-in-Hymanson.html

回答by Dan Temple

Given the following is your error:

鉴于以下是您的错误:

Unrecognized field "GaugeDeviceId" (Class GaugeDevice), not marked as ignorable

无法识别的字段“GaugeDeviceId”(类 GaugeDevice),未标记为可忽略

I'm pretty sure you need to do the same thing for the GaugeDeviceIdproperty as you've done for the SerialNoproperty.

我很确定你需要为GaugeDeviceId房产做同样的事情,就像你为SerialNo房产所做的那样。

@JsonProperty("SerialNo")
public String getSerialNo() {
    return this.serialNo;
}

@JsonProperty("GaugeDeviceId")
public int getGaugeDeviceId() {
    return this.gaugeDeviceId;
}

Here I have a quick test class that is not throwing errors.

在这里,我有一个不会抛出错误的快速测试类。

import org.codehaus.Hymanson.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();
        }
    }
}

And it outputs:

它输出:

123
456

EDIT: Version information

编辑:版本信息

Not that it matters, as I believe the above is all using some pretty standard stuff from Hymanson, I'm using version 1.9.13of the core-asland the mapper-asllibraries

这并不重要,因为我相信以上都是使用 Hymanson 的一些非常标准的东西,我使用的是core-aslmapper-asl库的1.9.13



EDIT: Client Provided

编辑:客户提供

I wonder if this is related to this issue? I believe the resolution is the configuration of dependencies that you're using.

我想知道这是否与这个问题有关?我相信分辨率是您使用的依赖项的配置。

I'm not sure, but I feel like I'm close with the following dependency setup (note I'm using maven)

我不确定,但我觉得我已经接近以下依赖项设置(注意我使用的是 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-Hymanson</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>

These links have provided the configuration information: Link 1, Link 2

这些链接提供了配置信息:Link 1, Link 2

回答by Christos

I had the same issue and I resolved it by changing the annotation import from:

我遇到了同样的问题,我通过更改注释导入来解决它:

com.fasterxml.Hymanson.annotation.JsonIgnoreProperties

to

org.codehaus.Hymanson.annotate.JsonIgnoreProperties

Didn't have to define any NamingStrategy or ObjectMapper.

不必定义任何 NamingStrategy 或 ObjectMapper。

回答by Balaji Boggaram Ramanarayan

You can ignore the unknown properties while deserializing by using an annotation at class level.

通过在类级别使用注释,您可以在反序列化时忽略未知属性。

For example :

例如 :

@JsonIgnoreProperties(ignoreUnknown=true)
class Foo{
 ...
}

The above snippet will ignore any unknown properties. ( Annotation import : org.codehaus.Hymanson.annotate.JsonIgnoreProperties )

上面的代码段将忽略任何未知的属性。(注释导入:org.codehaus.Hymanson.annotate.JsonIgnoreProperties)

回答by Bilal

I had the same issue and solved it by changing the annotation import from

我遇到了同样的问题并通过更改注释导入来解决它

com.fasterxml.Hymanson.annotation.JsonProperty

to

org.codehaus.Hymanson.annotate.JsonProperty

回答by Jorge Santos Neill

The solution that worked for me is the following

对我有用的解决方案如下

  1. Add the import

    import org.codehaus.Hymanson.map.DeserializationConfig;
    
  2. Configure the ObjectMapper

    objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);
    
  3. The complete solution

    ObjectMapper objectMapper = new ObjectMapper();      
    objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);     
    String jsonInString = objectMapper.writeValueAsString(eje); 
    Eje newElement = objectMapper.readValue(jsonInString, Eje.class); 
    this.eje = newElement;
    
  1. 添加导入

    import org.codehaus.Hymanson.map.DeserializationConfig;
    
  2. 配置对象映射器

    objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);
    
  3. 完整的解决方案

    ObjectMapper objectMapper = new ObjectMapper();      
    objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);     
    String jsonInString = objectMapper.writeValueAsString(eje); 
    Eje newElement = objectMapper.readValue(jsonInString, Eje.class); 
    this.eje = newElement;