2017-06-08 25 views
0
public ResponseEntity<Profile> getProfile(@PathVariable("id") String id){ 
    if(id != null){ 
    //getting CUSTOMER from DB 
    returning EntityProfile 

    if(CUSTOMER == null) 
    //getting data from webservice 
    returning Profile 
+2

歡迎來到SO!你準確的問題是什麼? https://stackoverflow.com/help/how-to-ask –

+1

您可以通過將方法封裝在表示方法返回值的單個類中,從而返回兩個不同的*對象*。您還需要使用Java語法。沒有關鍵字'返回'。 –

+0

如果'EntityProfile擴展配置文件',應該沒有問題。 –

回答

4

您可以使用泛型實現此功能。您尚未解釋ProfileEntityProfile之間的關係。因此,假設你有一些所謂BaseProfile這些2個個人對象擴展它們,你可以寫的返回類型爲:

public ResponseEntity <? extends BaseProfile> 

這樣,您就可以返回類型BaseProfile的任何對象。

如果ProfileEntityProfile的父級,則您的代碼(public ResponseEntity <Profile>)應該正常工作。但是,如果你想返回任何類型的對象,你可以改變返回類型爲:

public ResponseEntity <?> 
0

你可以嘗試創建一個包含EntityProfile屬性和配置文件屬性的新類:

public class CombinedClass{ 
    EntityProfile entity; 
    Profile profile; 
} 

然後讓你的方法返回一個CombinedClass與你需要返回的類。然後在您的接收端,您只需在檢索數據之前檢查實體或配置文件是否爲空。

public ResponseEntity<CombinedClass> getProfile(@PathVariable("id") String id){ 
    CombinedClass combined = new CombinedClass(); 
    if(id != null){ 
     combined.EntityProfile = //the EntityProfile you'll return 
    } 
    if(CUSTOMER == null){ 
     combined.profile= //the Profile you'll return 
    } 
    return combined; 
} 

多一點的代碼可以幫助你理解你是否需要它。

相關問題