Java根据参数返回相应类

问题初衷:如何根据参数变换方法的返回类型(参数为 类)

解决方案采用泛型解决(<T>)

解决方案:

记得在类上面加上组件扫描(@Component)

下面方法是放在工具类(例:YslRequestUtil) 

1 public <T> T  response(Object requestentity, T responseentity, String Url, String metodname){
2         //强转传递参数
3         T hfGetPolicyResponse = (T) resxstream.fromXML(result);
4         System.out.println(hfGetPolicyResponse);
5 
6         return hfGetPolicyResponse;
7     }        

调用工具类方法:

调用类加上依赖注入(@Resource)

 1 GetPolicyByRtPatRes getPolicyByAirLine = yslRequestUtil.response(getPolicyByAirLineReq, new GetPolicyByRtPatRes(), "http://www.baidu.com", "metodname"); 

原文地址:https://www.cnblogs.com/xiaohaojs/p/11760289.html