Generics with Spring RESTTemplate

后端 未结 4 1867
梦毁少年i
梦毁少年i 2020-11-28 23:31

I have a class like that:

public class Wrapper {

 private String message;
 private T data;

 public String getMessage() {
    return message;
 }

          


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 23:58

    The only thing I think you could do is creating a new class that extends Wrapper and uses model as a generic.

    class WrapperWithModel extends Wrapper{};
    
    WrapperWithModel response = restTemplate.getForObject(URL, WrapperWithModel.class);
    

    It's not the best solution, but at least you won't have to unmarshall manually the response.

提交回复
热议问题