com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “courses“ (clas

问题

2024-02-16 23:02:34.708 25114-25154/com.example.assistingagriculture W/System.err: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "courses" (class com.example.assistingagriculture.entity.User), not marked as ignorable (10 known properties: "address", "avatarUrl", "createTime", "id", "email", "nickname", "role", "password", "phone", "username"])

详细问题

笔者进行Android开发,使用com.fasterxml.jackson对数据进行服务端JSON数据进行解析,控制台报错。
Java核心代码
调用代码

parseResponse1(response, User.class);

解析代码

public static  <T> T parseResponse1(String jsonData, Class<T> valueType) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(jsonData, valueType);
    }

jsonData数据:

{"id":16,"username":"222","password":"b1c9f6ad0703828826e0bb01b281ef04","nickname":"TYF","email":"2","phone":"17513316004","address":"2","createTime":"2022-02-26T14:10:14.000+00:00","avatarUrl":"http://localhost:9090/file/8d966b0e6cf84fe191a72a58b8293b23.png","role":"ROLE_TEACHER","courses":null,"stuCourses":null}

User.class

public class User {
    private int id;
    private String username;
    private String password;
    private String nickname;
    private String email;
    private String phone;
    private String address;
    private Timestamp createTime;
    private String avatarUrl;
    private String role;

    // 省略构造方法、getter和setter方法

具体报错内容如下

2024-02-16 23:02:34.708 25114-25154/com.example.assistingagriculture W/System.err: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "courses" (class com.example.assistingagriculture.entity.User), not marked as ignorable (10 known properties: "address", "avatarUrl", "createTime", "id", "email", "nickname", "role", "password", "phone", "username"])
2024-02-16 23:02:34.708 25114-25154/com.example.assistingagriculture W/System.err:  at [Source: (String)"{"id":16,"username":"222","password":"b1c9f6ad0703828826e0bb01b281ef04","nickname":"TYF","email":"2","phone":"17513316004","address":"2","createTime":"2022-02-26T14:10:14.000+00:00","avatarUrl":"http://localhost:9090/file/8d966b0e6cf84fe191a72a58b8293b23.png","role":"ROLE_TEACHER","courses":null,"stuCourses":null}
2024-02-16 23:02:34.708 25114-25154/com.example.assistingagriculture W/System.err: "; line: 1, column: 298] (through reference chain: com.example.assistingagriculture.entity.User["courses"])
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:987)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1974)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1701)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1679)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:330)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3548)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3516)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.example.assistingagriculture.Utils.DataFormat.parseResponse1(DataFormat.java:152)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.example.assistingagriculture.Utils.DB.getUserById(DB.java:400)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.example.assistingagriculture.LoginActivity$GetUserTask.doInBackground(LoginActivity.java:290)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at com.example.assistingagriculture.LoginActivity$GetUserTask.doInBackground(LoginActivity.java:285)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at android.os.AsyncTask$3.call(AsyncTask.java:394)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2024-02-16 23:02:34.709 25114-25154/com.example.assistingagriculture W/System.err:     at java.lang.Thread.run(Thread.java:923)

解决方案

由于json数据中某字段在所需转换的实体类中未定义导致

解决方案一、客户端处理

对于客户端实体类,添加@JsonIgnoreProperties(ignoreUnknown = true)注解

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)

具体操作如下:
在这里插入图片描述

解决方案二、客户端处理

客户端在对象实体类中,添加无法被识别的字段。

解决方案三、服务端处理

服务端在对象实体类中,去除无法被识别的字段。

产生原因

问题产生的原因是因为 JSON 数据中包含了实体类 User 中未定义的属性 courses,而在使用 Jackson 库进行反序列化时,Jackson 默认情况下会严格匹配 JSON 数据和实体类的属性,因此导致了 UnrecognizedPropertyException 异常。

解决原因

问题的解决原因在于客户端的实体类 User 中使用了 @JsonIgnoreProperties(ignoreUnknown = true) 注解。这个注解告诉 Jackson 库在反序列化 JSON 数据时忽略未知的属性。

参考文献

产生原因与解释原因部分 部分内容参考chatgpt

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞滕人生TYF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值