java com.fasterxml.Jackson.databind.exc.UnrecognizedPropertyException:未识别字段

gv8xihay  于 2023-05-05  发布在  Java
关注(0)|答案(7)|浏览(217)

我遇到了一个反序列化问题:
这是我的班级:

public class Response {

    private Object ResObj;
    private int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
}

我想要反序列化JSON是:

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

我得到这个例外:

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"])

我不想补充:

@JsonIgnoreProperties(ignoreUnknown = true)

因为我想得到ResObj...
如果我添加注解,它会通过,但它会将其设置为null ..我不想这样

xxhby3vn

xxhby3vn1#

如果不想在bean中使用setter,而只使用fields和getter,那么可以使用ObjectMapper的可见性检查器来允许字段可见性。
类似以下内容:

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

plupiseo2#

您需要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;
    }

    // ...
}

以及:

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

JSON更改的原因是Jacksonbean序列化将反映在类上,当它看到getXyz()和setXyz()方法时,它将这些方法Map到名为“xyz”(而不是“Xyz”)的JSON字段。
我认为有几种方法可以覆盖这种行为,一种是使用Jackson注解之一。

cfh9epnr

cfh9epnr3#

我觉得你应该试试这个

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

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
}

它将解决您的UnrecognizedPropertyExceptions问题

yacmzcpb

yacmzcpb4#

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

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
}

使用此解决上述问题。

0yycz8jy

0yycz8jy5#

我已经使用Jackson库解决了这个问题。下面是我的代码片段。

**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.
zkure5ic

zkure5ic6#

我尝试了上面提到的所有方法,但在我的情况下,这是唯一的解决方案,作为回答here解决方案2.在这里,我在我的Sping Boot 应用程序中启用了@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));
    }
}
ntjbwcob

ntjbwcob7#

您需要为ResObj {“ClientNum”:“12345”,“ServerNum”:“78945”,“IdNum”:“020252”}中的信息定义另一个类。否则Jackson无法确定如何反序列化。

相关问题