我有两个对象应用程序和环境列表。我正在添加这些列表,但我将输出作为一个完整的应用程序列表,而不是其他整个环境列表。
我希望它以嵌套的形式存在,例如应用程序和环境的一个条目,而不是应用程序和环境的第二行等等
这是我的代码:
@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;
答案 0 :(得分: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对象。