Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add two different objects into a single object

Here is my function which is adding two different objects.

@GET
@Path("/getApplicationEnv")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public List<Object> getApplicationEnv(){

    List<ApplicationDTO> allApplication = applicationService.getAllApplication();                       
    List<Application> Applist = new ArrayList<Application>();

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

        a.setApplicationId(d.getApplicationId());
        a.setApplicationName(d.getApplicationName());
        a.setCreateTime(d.getCreateTime());
        a.setOwner(d.getOwner());   
        Applist.add(a);
    }   

    List<EnvironmentDTO> allEnvironments = environmentService.getAllEnvironments();

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

    for(EnvironmentDTO d: allEnvironments)
    {
        Environment e = new Environment();

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

    ArrayList<Object> obj= new ArrayList<Object>();
    obj.addAll(Applist);
    obj.addAll(Envlist);

    return obj;
}

Currently I am using ArrayList of Object for adding two objects, but I am getting following error:

*SEVERE: A message body writer for Java class java.util.ArrayList, and Java type java.util.List, and MIME media type application/xml was not found *

I have tried making a common parent class but I am having some attributes of both the class common so it is not possible to have a common parent class.

Can anyone please suggest me a way to achieve this ?

like image 722
Prats Avatar asked May 14 '26 15:05

Prats


1 Answers

you can try changing

    ArrayList<Object> obj= new ArrayList<Object>();
    obj.addAll(Applist);
    obj.addAll(Envlist);

to

ArrayList<MyModel> obj= new ArrayList<MyModel>();
MyModel mm = new MyModel();
mm.setVal1(Applist);
mm.setVal2(Envlist);
obj.add(mm);

UPDATE

class MyModel
{
   private ApplicationDTO appDTO;
   private EnvironmentDTO enDTO;

   //getters settters here
}

here MyModel is just a model class with two fields as per your requirement..
hope this helps

like image 63
Michael Shrestha Avatar answered May 18 '26 20:05

Michael Shrestha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!