简体   繁体   English

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

[英]Jackson Json Deserialisation: Unrecognized field “…” , not marked as ignorable

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将 propertyNames 更改为第一个大写字母 - 同样的错误
  2. adding @JsonProperty("SerialNo") to the property instantiation - same error@JsonProperty("SerialNo")到属性实例化 - 同样的错误
  3. adding @JsonProperty("SerialNo") to the corresponding getters - same error@JsonProperty("SerialNo")到相应的 getter - 同样的错误
  4. adding @JsonProperty("SerialNo") to the corresponding setters - same error@JsonProperty("SerialNo")到相应的 setter - 同样的错误
  5. adding @JsonProperty("SerialNo") to all of them (just for fun) - same error@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.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);
    }
}

I hope the error has its root here?我希望错误的根源在这里?

I had the same issue and I resolved it by changing the annotation import from:我遇到了同样的问题,我通过更改注释导入来解决它:

com.fasterxml.jackson.annotation.JsonIgnoreProperties

to

org.codehaus.jackson.annotate.JsonIgnoreProperties

Didn't have to define any NamingStrategy or ObjectMapper.不必定义任何 NamingStrategy 或 ObjectMapper。

I had the same issue and solved it by changing the annotation import from我遇到了同样的问题并通过更改注释导入来解决它

com.fasterxml.jackson.annotation.JsonProperty

to

org.codehaus.jackson.annotate.JsonProperty

Another thing to check out is PropertyNamingStrategy , which would allow Jackson to use "Pascal naming" and match JSON properties with POJO properties.另一件要检查的事情是PropertyNamingStrategy ,它允许 Jackson 使用“Pascal 命名”并将 JSON 属性与 POJO 属性匹配。 See f.ex here: http://www.javacodegeeks.com/2013/04/how-to-use-propertynamingstrategy-in-jackson.html请参阅此处的 f.ex: http : //www.javacodegeeks.com/2013/04/how-to-use-propertynamingstrategy-in-jackson.html

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 GaugeDeviceId property as you've done for the SerialNo property.我很确定您需要对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.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();
        }
    }
}

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 jackson, I'm using version 1.9.13 of the core-asl and the mapper-asl libraries这并不重要,因为我相信以上都是使用 jackson 的一些非常标准的东西,我使用的是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-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>

These links have provided the configuration information: Link 1 , Link 2这些链接提供了配置信息: Link 1 , Link 2

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.jackson.annotate.JsonIgnoreProperties ) (注释导入:org.codehaus.jackson.annotate.JsonIgnoreProperties)

The solution that worked for me is the following对我有用的解决方案如下

  1. Add the import添加导入

    import org.codehaus.jackson.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;

Make all your private variables and members to public .将所有私有变量和成员设为public Jackson works on public members variables. Jackson 处理公共成员变量。

public int gaugeDeviceId;
public Date utcInstallation; 
....

or add public Getters to private fields.或将公共 Getter 添加到私有字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jackson 与 JSON:无法识别的字段,未标记为可忽略 - Jackson with JSON: Unrecognized field, not marked as ignorable Spring Jackson-无法识别的字段“ response”在以下位置未标记为可忽略 - Spring Jackson - Unrecognized field \“response\” not marked as ignorable at jackson java无法识别的字段未标记为可忽略 - jackson java Unrecognized field not marked as ignorable JSON 到 Java 对象 - 无法识别的字段,未标记为可忽略 - JSON to Java object - Unrecognized field, not marked as ignorable 未被识别的字段,未标记为可忽略 - unrecognized field, not marked as ignorable JSON:无法识别的字段“值”( <objectClass> ),没有标记为可忽略的 - JSON : Unrecognized field “value” (<objectClass>), not marked as ignorable 无法识别的字段类未标记为可忽略 - Unrecognized field class not marked as ignorable 将 JSON 映射到 POJO 进行处理 - 无法识别的字段未标记为可忽略 - Map JSON to POJO For Processing - Unrecognized field not marked as ignorable 解析json字符串时,无法识别的字段(未标记为可忽略) - Unrecognized field , not marked as ignorable ,when parsing json string Java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“”不可标记 - Java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" not marked as ignorable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM