簡體   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.)

如何從JSON REST請求中檢索ArrayList / List並轉換為POJO對象(PO​​JO變量之一是列表類型)。

//域類

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

    @JsonProperty("name")
    private String name;

    @JsonProperty("type")
    private String type;

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

//控制器類

@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.
    }}

您可以使用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>>() {};

例如,您可以參考問題答案

參考ParameterizedTypeReference

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM