1

I have an execute method that I've created where I pass in my restTemplate instance along with the class obj. However, I'm getting some warnings about unchecked types. I can't seem to figure out how to do this with generics. Here's what I have so far:

public class RepositoryUtils {
 private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryUtils.class);
 public static ResponseEntity execute(String url, RestTemplate restTemplate, Class generic) {
 LOGGER.info("GET: {}", url);
 ResponseEntity response = null;
 try {
 response = restTemplate.exchange(url, HttpMethod.GET, RequestHelper.getGzipHttpEntity(), generic);
 } catch (Exception e) {
 LOGGER.error("RestTemplate: {} - {}", url, e.getMessage());
 }
 return response;
 }
}

And here is how I make a call to my static execute:

ResponseEntity<Channels> response = RepositoryUtils.execute(channelUrlFinal, restTemplate, Channels.class);
asked Aug 16, 2016 at 17:14
2
  • Please let us know the exact error and the location. My guess is you need to change the method signature to public static ResponseEntity execute(String url, RestTemplate restTemplate, Class<?> generic) { Commented Aug 16, 2016 at 17:20
  • @jhyot I'm not getting an error but two warnings instead. When I'm calling my execute method I get an unchecked assignment warning since I'm expecting a ResponseEntity<Channels> even though my method only returns ResponseEntity Commented Aug 16, 2016 at 17:35

1 Answer 1

2

Here is what you could try as signature of your method:

public static <T> ResponseEntity<T> execute(String url, RestTemplate restTemplate, 
 Class<T> generic) {
answered Aug 16, 2016 at 17:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.