question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use generic class as 'asObject' parameters

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:5

github_iconTop GitHub Comments

3reactions
petrdvorakcommented, Jul 31, 2017

@andrekat No, please read my original post. I have a generic ApiRequest<T> and ApiResponse<T> classes and I need to serialize these using Unirest. However, there is no such thing as ApiResponse<SomeClass>.class, hence you need to use TypeReferece as I demonstrated in my workaround. It would be nice to have .asObject(t) accepting TypeReference as parameter and using ObjectMapper instance associated with Unirest…

2reactions
andrekatcommented, Jul 31, 2017

I am using Unirest with generic class calls. Is this what are you looking for?

public <T> T doGet(String path, Class<T> className) throws UnirestException {

        String url = (path.startsWith("http") || path.startsWith("www")) ? path : base_url + path;

        Main.log.debug("doGet: " + url);

        if (!path.startsWith("/")) {
            path = StringUtils.overlay(path, "/", 0, 0);
        }

        return Unirest.get(this.base_url + path).header("accept", "application/json").asObject(className).getBody();
    }

Example of calling this method:

ResponseMusicContent response = OneClient.getInstance().doGet(url, ResponseMusicContent.class);

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found