1

How to generify this method? I'm trying to receive a class in my parameter and return a object from that class.

However, Java says that "any.class" int the return is unknown.

Am I doing something wrong with generics? How can I generify this method to receive a ResponseEntity and a Class and then return a Object from that class?

public static <T> T parseToJson(Class<T> any, ResponseEntity responseEntity) {
    try {
        return new ObjectMapper().readValue(responseEntity.getBody().toString(), any.class);
    } catch (IOException e) {
        logger.error("Error: " + e.getMessage());
    }
}

One more doubt: If I want to return a array of objects, I used to do

 return Arrays.asList(new ObjectMapper().readValue(responseEntity.getBody().toString(), ResponseListCameras[].class));

but I'm trying to do

return Arrays.asList(new ObjectMapper().readValue(responseEntity.getBody().toString(), any[])); 

and it seems to expect an expression. What can I do?

FearX
  • 317
  • 3
  • 16

2 Answers2

3
  1. Change any.class to any since any is already a Class<T> instance.
  2. Rethrow the exception unless you want to return something default after the try-catch statement.

Otherwise, you will get 2 compilation errors.

  1. Don't use raw types. ResponseEntity has a type parameter.
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • What should I do with ResponseEntity? – FearX Jul 26 '19 at 14:14
  • Edited my post to include the array question – FearX Jul 26 '19 at 14:25
  • @FearX you can't make `any[]` out of `any`, you should pass an array-based class instead (e.g. `parseToJson(ResponseListCameras[].class, actualResponseEntity)`) – Andrew Tobilko Jul 26 '19 at 14:34
  • @AndrewTobilko yes, I'm writing another method. That question is about(i don't know how it is called in engish) overcharging that method to accept an array, too – FearX Jul 26 '19 at 14:38
  • @AndrewTobilko i'll pass an any[] in this case – FearX Jul 26 '19 at 14:40
  • @FearX you probably meant "overloading". The thing is you aren't overloading anything: the method remains the same except for the type. And I don't know any a nice way to turn `any` into `any[]` – Andrew Tobilko Jul 26 '19 at 14:41
  • @FearX have a look here https://stackoverflow.com/questions/4901128/obtaining-the-array-class-of-a-component-type – Andrew Tobilko Jul 26 '19 at 14:42
  • @AndrewTobilko yes, overloading. Thank you for that. I think I found a nice way, can you say what you think about it? It's the "obi-wan" answer. https://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects – FearX Jul 26 '19 at 14:43
  • @FearX yes, it might work. But, for the second method, you need to change its name. You can't have 2 methods with the same signature and name. – Andrew Tobilko Jul 26 '19 at 14:45
  • Yeah, sure. That makes perfect sense to me – FearX Jul 26 '19 at 14:46
0

Here you pass a String provided from getBody().toString() to deserialize the json.
The approach looks weird because ResponseEntity is a generic class and that ResponseEntity.getBody() returns an instance of the generic.
So you should not even use directly Jackson here.

ResponseEntity is typed as long as you use it correctly.

For example :

ResponseEntity<Foo> responseEntityFoo = template.getForEntity("/getAnUrl...", Foo.class);
Foo foo = responseEntityFoo.getBody();   

So you should have the JSON deserialized just like that.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Fantastic, @davidxxx. But how should I return an List without using directly Jackson? – FearX Jul 26 '19 at 14:46
  • I removed the second part. That is helpless. If you want to deserialize it, it means that your service returns a JSON in its body. Just use it. If you receive a JSON array , just specify it in your invocation : `ResponseEntity responseEntities = template.getForEntity("url...", Foo[].class); List = Arrays.asList(responseEntities.getBody());` – davidxxx Jul 26 '19 at 14:53
  • Arrays.asList is dropping ClassCastException to me java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object; – FearX Jul 26 '19 at 15:02
  • In fact, all "getBody()" are dropping the same exception – FearX Jul 26 '19 at 15:05
  • do you have any idea about this exception? – FearX Jul 26 '19 at 17:18
  • No Idea, really. – davidxxx Jul 27 '19 at 07:09