3

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The output is now: {"services":[{"serviceName":"telnet","servicePort":21},{"serviceName":"SSH","servicePort":22}],"ipAddress":"192.168.0.4","servername":"serverOne"}. So working perfectly. As .net developer I'm starting to love learning Java. Thank you verymuch. One little question. Is there any way of ordering the output?. Services at the end?
Ok I found it-> JsonPropertyOrder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.