简体   繁体   English

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段

[英]com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field

i got a deserialization problem:我有一个反序列化问题:

This is my class:这是我的课:

public class Response {

    private Object ResObj;
    private int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
} 

the JSON i want to deserialize is:我要反序列化的 JSON 是:

{"ResObj":{"ClientNum":"12345","ServerNum":"78945","IdNum":"020252"},"ResInt":0}

I get this exception:我得到这个例外:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ResObj" , not marked as ignorable (0 known properties: ])
 at [Source: java.io.StringReader@1f758500; line: 1, column: 20] (through reference chain: ["ResObj"])

I don't want to add:我不想补充:

@JsonIgnoreProperties(ignoreUnknown = true)

because I want to get the ResObj...因为我想得到 ResObj ...

if I add the annotation, it pass but it will set it as null .. which I don't want.如果我添加注释,它会通过但它会将其设置为 null .. 我不想要。

If you don't want to have a setter in your bean and only use fields and getters, you can use the visibility checker of ObjectMapper to allow field visibility.如果你不想在你的 bean 中有一个 setter 并且只使用字段和 getter,你可以使用ObjectMapper的可见性检查器来允许字段可见性。
Something like following:类似于以下内容:

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setVisibility(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));

You need Setter methods to allow Jackson to set the properties, and you need to change the fields in the json to begin with a lower case letter:您需要 Setter 方法来允许 Jackson 设置属性,并且您需要将 json 中的字段更改为以小写字母开头:

public class Response {

    private Object ResObj;
    private int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public void setResObj(Object ResObj) {
        this.ResObj = ResObj;
    }

    // ...
}

and:和:

{"resObj":{"clientNum":"12345","serverNum":"78945","idNum":"020252"},"resInt":0}

The reason for the JSON change is that the Jackson bean serialisation will reflect over the class, and when it sees getXyz() and setXyz() methods will map these to a Json filed names "xyz" (and not "Xyz"). JSON 更改的原因是 Jackson bean 序列化将反映在类上,当它看到 getXyz() 和 setXyz() 方法时,会将这些映射到 Json 文件名称“xyz”(而不是“Xyz”)。

I think there are several ways to override this behaviour, one is to use the one of the Jackson annotations.我认为有几种方法可以覆盖这种行为,一种是使用 Jackson 注释之一。

I think you should try this我想你应该试试这个

public class Response {
    @JsonProperty
    private Object ResObj;
    @JsonProperty
    private int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
} 

It will resolve your issue with UnrecognizedPropertyExceptions它将通过 UnrecognizedPropertyExceptions 解决您的问题

public class Response {
    public Object ResObj;
    public int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
} 

Use this to resolve the above issue.使用它来解决上述问题。

I have sorted this problem using the Jackson library.我已经使用 Jackson 库对这个问题进行了排序。 Here is my code snippet.这是我的代码片段。

**Main Class with JSON String in all lower case:**

 public class MainClass {

public static void main(String[] args) throws JsonParseException, 
    JsonMappingException, IOException {

    String jsonStr = "{\r\n" + "    \"resObj\": {\r\n" + "      \"clientNum\": 
               \"12345\",\r\n"
            + "     \"serverNum\": \"78945\",\r\n" + "      \"idNum\": 
          \"020252\"\r\n" + "   },\r\n"
            + " \"resInt\": 0\r\n" + "}";

    ObjectMapper mapper = new ObjectMapper();

    MyPojo details = mapper.readValue(jsonStr, MyPojo.class);

    System.out.println("value of clientNum: " + details.getResObj().getClientNum());
    System.out.println("value of getServerNum: " + 
               details.getResObj().getServerNum());
    System.out.println("value of getIdNum: " + details.getResObj().getIdNum());
    System.out.println("value of getResInt: " + details.getResInt());

} }

**MyPojo Class:**

 public class MyPojo {
private ResObj resObj;

private String resInt;

public ResObj getResObj() {
    return resObj;
}

  public String getResInt() {
       return resInt;   } }

**ResObj class:**


public class ResObj {
private String serverNum;

private String idNum;

private String clientNum;

public String getServerNum() {
    return serverNum;
}

public String getIdNum() {
    return idNum;
}

public String getClientNum() {
    return clientNum;
} }

**RESULT**

  value of clientNum: 12345
  value of getServerNum: 78945
  value of getIdNum: 020252
  value of getResInt: 0

NOTE: I have removed Setters in classes & there is no effect on the result.

I tried all the ways mentioned above but in my case, this is the only solution that worked as answered here Solution 2. Here I have enabled @EnableWebMvc in my spring boot application.我想以上,但在我的案件中提到的所有方面,这仅仅是担任回答解决解决方案2.在这里,我已经启用@EnableWebMvc在我的春天启动应用程序。

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private ObjectMapper objectMapper;// created elsewhere
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // this will add a 2nd MappingJackson2HttpMessageConverter 
        converters.add(new MappingJackson2HttpMessageConverter(this.objectMapper));
    }
}

You need to define another class for the information within ResObj {"ClientNum":"12345","ServerNum":"78945","IdNum":"020252"}.您需要为 ResObj {"ClientNum":"12345","ServerNum":"78945","IdNum":"020252"} 中的信息定义另一个类。 Otherwise jackson cannot determine how to deserialize.否则杰克逊无法确定如何反序列化。

暂无
暂无

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

相关问题 解决com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段 - resolving com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“ g” - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “g” Jackson 反序列化错误:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别 - Jackson deserialization error: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field 无法使用杰克逊,com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException将xml绑定到pojo:无法识别的字段 - can not bind xml to pojo using jackson, com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException 对象映射器给出异常:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段 - Object Mapper giving Exception: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field 引起:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“Status” - Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “Status” com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“消息”异常 - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “message” exception Java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“”不可标记 - Java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" not marked as ignorable com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“user_activity” - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "user_activity"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM