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"); 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM