简体   繁体   English

如何从json rest请求中检索Arraylist / list并转换为pojo对象(pojo变量之一是列表类型。)

[英]How to retrieve Arraylist/list from json rest request and convert into pojo object (one of pojo variable is of list type.)

How to retrieve ArrayList / List from JSON REST request and convert into POJO object (one of POJO variable is of list type). 如何从JSON REST请求中检索ArrayList / List并转换为POJO对象(PO​​JO变量之一是列表类型)。

// Domain class //域类

@Service
@JsonIgnoreProperties(ignoreUnknown = true)
public class DocumentProps {

    @JsonProperty("name")
    private String name;

    @JsonProperty("type")
    private String type;

    @JsonProperty("properties")  // list variable
    private List properties;

// controller-class //控制器类

@RestController
public class springteamplate {

@Autowired
    DocumentProps docprops;

@GetMapping(value = "/get")
    private  void getEmployees()
    {
        final String uri = "http://xyz";        
        docprops = RestTemplateBuild.template.getForObject(uri, DocumentProps.class);
        System.out.println(docprops.getName()); // able to retrieve name
        System.out.println(docprops.getType());  // able to retrieve type
        System.out.println(docprops.getPrpopertie());  // How to retrieve list of properties.
    }}

You can get the collection types or generic types binding using the ParameterizedTypeReference . 您可以使用ParameterizedTypeReference获取集合类型或泛型类型绑定。

The purpose of this class is to enable capturing and passing a generic Type.In order to capture the generic type and retain it at runtime, you need to create a subclass (ideally as anonymous inline class) as follows:

ParameterizedTypeReference<List<String>> typeRef = new ParameterizedTypeReference<List<String>>() {};

For example you can reference to the answer on question 例如,您可以参考问题答案

Reference ParameterizedTypeReference 参考ParameterizedTypeReference

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM