2017-06-08 52 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; 
} 

多一点的代码可以帮助你理解你是否需要它。

相关问题