mercredi 28 avril 2021

JAVA - Reflection onto Generic Type [duplicate]

i want do do a POST Request using Spring REST Template and a Generic Type for the ResponseEntity. For this i need to do a reflection onto the class i want to use for the ResponseEntity. How can i achieve this?

This is my code so far:

public <T> T post(String url, Map<String, Object> body) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

    //build Request
    HttpEntity<Map<String, Object>> entity = new HttpEntity<>(body, headers);
    //Send request
    ResponseEntity<T> response = this.restTemplate.postForEntity(url, entity, T.class);
}




Aucun commentaire:

Enregistrer un commentaire