简体   繁体   中英

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

// 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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