Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - Returning JSON Object with List<Custom> property

I have the following mapping on my spring mvc controller:

   @RequestMapping(value = "/servers", method = RequestMethod.GET)

    public @ResponseBody Server getServers(@RequestParam(value="password", required=false) String password)
    {

        Server server = new Server();
        server.setIpaddress("192.168.0.4");
        server.setServername("serverOne");

        List<Service> list = new ArrayList<Service>();   

        Service s1 = new Service();
        s1.setServiceName("telnet");
        s1.setServicePort(21);
        list.add(s1);
        s1= new Service();
        s1.setServiceName("SSH");
        s1.setServicePort(22);
        list.add(s1);

        server.SetServices(list);
        return server;

    }

It should return a server class in json , with one filled property matching List but is doesn't show anything. This are the involved classes:

Class Server:

package es.landesoft.mvctesting.JavaBeans;

import java.util.List;


public class Server {


    private String ipaddress;
    private String serverName;
    private List<Service> services;

    public void setIpaddress(String value)
    {
        this.ipaddress = value;     
    }

    public String getIpAddress()
    {
        return this.ipaddress;  
    }

    public void setServername (String value)
    {       
        this.serverName= value;
    }

    public String getServername()
    {
        return this.serverName;     
    }

    public void SetServices(List<Service> services)
    {
        this.services= services;
    }

    public List<Service> GetServices()
    {
        return this.services;
    }

}

Class service:

package es.landesoft.mvctesting.JavaBeans;

public class Service
{
    private String serviceName;
    private int servicePort;

    public void setServiceName(String value)
    {
        this.serviceName= value;
    }

    public String getServiceName(){
        return this.serviceName;
    }

    public void setServicePort(int value)
    {
        this.servicePort=value;
    }

    public int getServicePort()
    {
        return this.servicePort;
    }

}

The Json Output is:

{"servername":"serverOne","ipAddress":"192.168.0.4"}

No trace of the List property. What am I doing wrong.

like image 618
Carlos Landeras Avatar asked Feb 24 '26 10:02

Carlos Landeras


1 Answers

do like this

Change your Server model like this.

public class Server {

   private List<Service> services = new ArrayList<Service>();
}

And Add like this.

Server server = new Server();
server.setIpaddress("192.168.0.4");
server.setServername("serverOne");
Service s1 = new Service();
s1.setServiceName("telnet");
s1.setServicePort(21); 
server.GetServices().add(s1); //Add like this.

Note: Maintain the java naming conventions.

 public List<Service> GetServices(){}

should be public List<Service> getServices()

The same for setters too.

like image 182
Prabhakaran Ramaswamy Avatar answered Feb 27 '26 00:02

Prabhakaran Ramaswamy



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!