HTTP Servlet中的ArrayList问题

时间:2014-04-23 23:25:04

标签: java apache rest tomcat servlets

我正在使用Floodlight REST API来监控mininet中创建的虚拟网络。我的目标是使用Apache Tomcat Web服务器和HTTP Servlet在Web浏览器上显示交换机的所有交换机,主机和统计信息的arraylist。应用程序成功显示所有交换机和主机,但在添加交换机的统计信息时失败。

当我将JSON字符串映射到java对象时,服务器会在此行中返回错误:

ArrayList<Switch> queues = mapper.readValue(queueJson, new TypeReference<ArrayList<Switch>>() {
    });

错误是:

HTTP status 500 - can not deserialize instance of java.util.arraylist out of start_object token

我没有切换统计信息(队列)部分(只有主机和设备)测试它,一切正常,但是当我添加队列ArrayList时,它返回上面提到的错误。

我该如何解决这个问题?我的代码如下所示。提前致谢

package core;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.restlet.data.MediaType;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;

import pojos.Device;
import pojos.Switch;

@WebServlet("/PrintInfo")
public class PrintInfo extends HttpServlet {
private static final long serialVersionUID = 1L;

public PrintInfo() {
    super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    // create ClientResource object

    // List at the switches in the network
    ClientResource cResourceSwitches = new ClientResource("http://127.0.0.1:8080/wm/core/controller/switches/json");
    StringWriter sWriterSwitches = new StringWriter();

    // List all the devices (hosts) in the network
    ClientResource cResourceDevices = new ClientResource("http://127.0.0.1:8080/wm/device/");
    StringWriter sWriterDevices = new StringWriter();

    // List the statistics of the switches in the network
    ClientResource cResourceQueues = new ClientResource("http://127.0.0.1:8080/wm/core/switch/all/queue/json");
    StringWriter sWriterQueues = new StringWriter();

    // get JSON data about switches; the data is put in a string writer
    try {

        // Getting data from Floodlight as a JSON string

        cResourceSwitches.get(MediaType.APPLICATION_JSON).write(sWriterSwitches);
        cResourceDevices.get(MediaType.APPLICATION_JSON).write(sWriterDevices);

        cResourceQueues.get(MediaType.APPLICATION_JSON).write(sWriterQueues);

    } catch (ResourceException e) {
        request.setAttribute("error", "Connection with FLoodLight failed!");
        request.getRequestDispatcher("WEB-INF/connectionError.jsp").forward(request, response);
        return;
    }

    // put data from string writer into a string object
    String switchesJson = sWriterSwitches.toString();
    String devicesJson = sWriterDevices.toString();
    String queueJson = sWriterQueues.toString();

    // map JSON data to Java objects
    // ObjectMapper converts between JSON - Java
    ObjectMapper mapper = new ObjectMapper();


    ArrayList<Switch> switches = mapper.readValue(switchesJson, new TypeReference<ArrayList<Switch>>() {
    });
    ArrayList<Device> devices = mapper.readValue(devicesJson, new TypeReference<ArrayList<Device>>() {
    });

    ArrayList<Switch> queues = mapper.readValue(queueJson, new TypeReference<ArrayList<Switch>>() {

    });

    // put objects in the request so we can use them later in the JSP
    request.setAttribute("switches", switches);
    request.setAttribute("devices", devices);
    request.setAttribute("queues", queues);

    // redirect to the jsp
    request.getRequestDispatcher("WEB-INF/showInfo.jsp").forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
        IOException {

}

}

1 个答案:

答案 0 :(得分:1)

解决。 Switch类在&#34; ArrayList&#34;无法用于切换统计信息。必须实现一个新类,它返回

中的值
http://127.0.0.1:8080/wm/core/switch/all/queue/json 

URI。