Unable to use generic class as 'asObject' parameters
See original GitHub issueWhen I work with Spring RestTemplate
class, I can use ParametrizedTypeReference
instance to provide generic class to deserialization, like so:
ResponseEntity<ApiResponse<Payment>> response = template.exchange(request, new ParameterizedTypeReference<ApiResponse<Payment>>() {});
This is really nicely integrated in the overall code.
It would be nice to include a chapter on how to achieve this with Unirest. For example:
// Generic response type
HttpResponse response = Unirest.post(uri)
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.body(body)
.asString();
ObjectMapper mapper = new ObjectMapper();
TypeReference<ApiResponse<Payment>> type = new TypeReference<ApiResponse<Payment>>() {};
ApiResponse<Payment> responseWrapper = mapper.readValue(response.getRawBody(), type);
Ideally, it would be nice to be able to provide some API where I can actually use type reference as a parameter, for example:
HttpResponse response = Unirest.post(uri)
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.body(body)
.asObject(new TypeReference<ApiResponse<Payment>>() {});
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:5
Top Results From Across the Web
c# - Pass An Instantiated System.Type as a Type Parameter ...
Generic arguments must be resolvable at Compile time as either 1) a valid type or 2) another generic parameter. There is no way...
Read more >Restrictions on Generics (The Java™ Tutorials > Learning the ...
To use Java generics effectively, you must consider the following restrictions: ... Cannot Declare Static Fields Whose Types are Type Parameters; Cannot Use...
Read more >4.1. Generic Types - Java in a Nutshell, 5th Edition [Book]
A generic type is defined using one or more type variables and has one or more methods that use a type variable as...
Read more >Java Generics Tutorial - HowToDoInJava
Java Generics is a technical term denoting a set of language features related to the definition and use of generic types and methods...
Read more >Object orientation - The Apache Groovy programming language
When defining classes and methods, it is possible to use a type parameter and create a generic class, interface, method or constructor. Usage...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@andrekat No, please read my original post. I have a generic
ApiRequest<T>
andApiResponse<T>
classes and I need to serialize these using Unirest. However, there is no such thing asApiResponse<SomeClass>.class
, hence you need to useTypeReferece
as I demonstrated in my workaround. It would be nice to have.asObject(t)
acceptingTypeReference
as parameter and usingObjectMapper
instance associated with Unirest…I am using Unirest with generic class calls. Is this what are you looking for?
Example of calling this method:
ResponseMusicContent response = OneClient.getInstance().doGet(url, ResponseMusicContent.class);