0

我有两个对象应用程序和环境列表。我正在添加这些列表,但我得到的输出是一个完整的应用程序列表,而不是另一个完整的环境列表。

我希望它采用嵌套形式,例如应用程序和环境的一个条目,而不是应用程序和环境的第二行等

这是我的代码:

@GET
@Path("/test")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public ArrayList<MyModel> Test(){

    System.out.println("inside test");

    List<ApplicationDTO> allApplication = applicationService.getAllApplication();

    List<Application> Applist = new ArrayList<Application>();
    List<Environment> Envlist = new ArrayList<Environment>();

    for(ApplicationDTO d: allApplication)
    {
        Application a = new Application();

        a.setApplicationDescription(d.getApplicationDescription());
        a.setApplicationId(d.getApplicationId());
        a.setApplicationName(d.getApplicationName());
        a.setAssignedTo(d.getAssignedTo());
        a.setCreatedBy(d.getCreatedBy());
        a.setCreateTime(d.getCreateTime());
        a.setModifiedTime(d.getModifiedTime());
        a.setOwner(d.getOwner());

        Environment e = new Environment();

        for(EnvironmentDTO en: d.getEnvironment())
        {
            e.setEnvironmentId(en.getEnvironmentId());
            e.setEnvironmentName(en.getEnvironmentName());
            e.setOwner(en.getOwner());
            e.setCreateTime(en.getCreateTime());

            Envlist.add(e);
        }

        Applist.add(a);
    }

    ArrayList<MyModel> obj= new ArrayList<MyModel>();
    MyModel mm = new MyModel();
    mm.setAppDTO(Applist);
    mm.setEnDTO(Envlist);
    obj.add(mm);
    return obj; 
    }

编辑

我改变了我的课程,我在应用程序内添加了环境列表,现在我正在获取应用程序内所有环境的列表,但我需要一个应用程序内的一个环境,另一个在下一个应用程序内,依此类推。我错过了什么?

    public List<Application> Test(){

    System.out.println("inside test");

    List<ApplicationDTO> allApplication = applicationService.getAllApplication();

    List<Application> Applist = new ArrayList<Application>();
    List<Environment> Envlist = new ArrayList<Environment>();

    for(ApplicationDTO d: allApplication)
    {           
        Application a = new Application();

        a.setApplicationDescription(d.getApplicationDescription());
        a.setApplicationId(d.getApplicationId());
        a.setApplicationName(d.getApplicationName());
        a.setAssignedTo(d.getAssignedTo());
        a.setCreatedBy(d.getCreatedBy());
        a.setCreateTime(d.getCreateTime());
        a.setModifiedTime(d.getModifiedTime());
        a.setOwner(d.getOwner());

        for(EnvironmentDTO en: d.getEnvironment())
        {
            Environment e = new Environment();

            e.setEnvironmentId(en.getEnvironmentId());
            e.setEnvironmentName(en.getEnvironmentName());
            e.setOwner(en.getOwner());
            e.setCreateTime(en.getCreateTime());
            Envlist.add(e);
        }

        a.setEnvlists(Envlist);         
        Applist.add(a);
    }
    return Applist;
4

1 回答 1

1

您应该创建一个包含应用程序和环境列表的对象,例如 ApplicationWithEnvironments。

    List<ApplicationWithEnvironments > AppWithEnvlist = new ArrayList<ApplicationWithEnvironments >();        

    for(ApplicationDTO d: allApplication) {
        ApplicationWithEnvironments appWithEnv = new ApplicationWithEnvironments();

        Application a = new Application();

        a.set...

        appWithEnv.setApplication(a);


        for(EnvironmentDTO en: d.getEnvironment())
        {
            Environment e = new Environment();
            e.set...
            appWithEnv.addEnvironment(e);
        }

        AppWithEnvList.add(appWithEnv);
    }

    ArrayList<MyModel> obj= new ArrayList<MyModel>();
    MyModel mm = new MyModel();
    mm.setAppWithEnvList(AppWithEnvList);
    obj.add(mm);
    return obj; 

或者您可以在 Application 对象中有一个 Environments 列表,因此您不再需要新的 ApplicationWithEnvironments 对象。

于 2013-07-01T11:42:42.683 回答