【问题标题】:Object Mapper giving Exception: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field对象映射器给出异常:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段
【发布时间】:2019-12-16 12:21:21
【问题描述】:

我正在使用 Spring Boot 应用程序,在使用 Postman 的 GET 请求访问“/test/api”休息端点时,出现以下错误:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 无法识别的字段“用户名”(类 com.example.MyPojo),未标记为可忽略 (0 个已知属性:])

我尝试使用的服务产生以下格式的响应。

@Getter
@Setter
public class MyResponse extends MyPojo {

    int responseCode;
    String responseMessage;
    List<MyPojo> output;
}
public class MyPojo{
}
public class User extends MyPojo {

    private String id;

    @NotBlank
    private String userName;

    private String companyId;
}

我的 Controller 类如下所示。

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@RestController
@RequestMapping("/test")
public class SampleRestController {

    @GetMapping("/api")
    public MyResponse testApi(){

        RestTemplate restTemplate = new RestTemplate();
        String url="http://<Domain>:8085/users/active";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> entity = new HttpEntity<String>("header",headers);
        final ResponseEntity<String> responseEntity = restTemplate.exchange( url, HttpMethod.GET, entity, String.class );

        MyResponse myResponse = null;
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility( PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        mapper.enable( DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
        try {
            myResponse = mapper.readValue(responseEntity.getBody(), MyResponse.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return myResponse;
    }

}

请指出我的错误,我无法弄清楚。任何帮助将不胜感激。

【问题讨论】:

  • 我尝试将 @JsonIgnoreProperties(ignoreUnknown = true) 放在 MyPojo 类上,结果如下所示。 { "responseCode": 200, "responseMessage": "Success", "output": [ {}, {}, {} ] } 在我检查 myResponse 进行调试时,我看到 MyPojo 的值显示“类没有字段”。如何将用户属性映射到 MyPojo?
  • MyPojo 为空。您如何将某些东西映射到没有属性的类?
  • @MarkD - 要映射用户属性,您需要更改 List 输出;列出 输出;在 MyResponse 模型中,因为 MyPojo 是空的
  • 您的用户 JSON 字符串包含 userName 字段,但在 MyPojo 类中找不到。

标签: java json rest spring-boot objectmapper


【解决方案1】:

要映射用户属性,您需要有 UserResponse-

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserResponse extends MyPojo {

    int responseCode;
    String responseMessage;
    List<User> output;
}

同样,对于 Product,您需要 ProductResponse-

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProductResponse extends MyPojo {

    int responseCode;
    String responseMessage;
    List<Product> output;
}

另外,如果当前不使用,定义@Getter 和@Setter 注解。

@Getter
@Setter
public class User extends MyPojo {

    private String id;

    @NotBlank
    private String userName;

    private String companyId;
}

【讨论】:

  • 实际上我想保持“MyResponse”的灵活性,因为我想使用相同的类来响应其他类,如“产品”、“联系人”等,我所有的 pojo 类都扩展了 MyPojo。是的,我已经为所有 Pojo 类定义了 Getter 和 Setter 注释。
  • 要实现这一点,您需要有不同的 DTO,即 ProductResponse 具有 List,ContactsResponse 具有 List 等。使用 Single MyResponse 是不可能的,也不建议使用。或者,您可以将响应转换为 JSON,并根据您的要求使用输出键映射到 List 或 List。在这种情况下,也不需要 MyResponse。第一个,即使用不同的 DTO 是首选。
  • 感谢 Akash 和 MarkD :) 我按照您的建议使用了输出密钥并为我工作。
猜你喜欢
  • 2020-10-30
  • 2014-06-21
  • 2016-04-13
  • 2016-01-26
  • 2021-02-11
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 2019-04-06
相关资源
最近更新 更多