1

I want to extract json object with restassured, and response is generic like:

@Data
public class ExtractBase<T> {
    private Class<T> result; // here I can expect different classes 
    private String targetUrl;
    private Boolean success;
    private String error;
    private Boolean unAuthorizedRequest;
    private Boolean __abp;

}

and I want extract it and get result each time with different class:

.extract().body().as(ExtractBase.class).getResult(); // and here I want to have possibility to choose class which should be exctracted depends on my request

I have tried to use TypeToken but with no result :(

any tips for extracting generic classes from JSON responses?

Sweta Jain
  • 3,248
  • 6
  • 30
  • 50
Vorgan
  • 11
  • 1

1 Answers1

0

Use ObjectMapper to map from Objects response to custom type. Example objectMapper.readValue(json.getBoody(), YourType.class) Remember, that objectMaper mustbe register module on methot objectMaper

private final ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();

and your response shouldby

private T result;  - witout "class"
turo
  • 59
  • 1
  • 9
  • hmm ill try it, for now i get workaround with extending my class: public class PostRejectHandler extends ExtractBase{} and my extract base class is: public class ExtractBase { private T result; } – Vorgan May 10 '22 at 17:22