获取HTTP请求的响应代码 - Java

时间:2015-11-15 00:39:42

标签: java rest http

这是我的问题:我想获取客户端发出的HTTP请求的响应代码。

这是我的代码:

@GET
@Produces({MediaType.TEXT_XML,  MediaType.APPLICATION_JSON ,  MediaType.APPLICATION_XML })
public List<Person> getPersonsBrowser() throws IOException {

    int code = 0;
    try
    {
        URL url = new URL(uriInfo.getAbsolutePath().toString());
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        code = connection.getResponseCode();  

    }
    catch(Exception e)
    {

    }

    System.out.println("code: "+code);
    System.out.println("Request #1: "+ request.getMethod() + " /"+uriInfo.getPath() + "Accept: ");

    //System.out.println("Getting list of people...");
    List<Person> people = Person.getAll();

    //System.out.println("----"+people.get(1));
    return people;
}

我的问题是结果是无限循环,如下所示:

> Request #1: GET /personAccept: 
code: 200
Request #1: GET /personAccept: 
code: 200
Request #1: GET /personAccept: 
code: 200
Request #1: GET /personAccept: 
code: 200
Request #1: GET /personAccept: 
code: 200
Request #1: GET /personAccept: 
code: 200
Request #1: GET /personAccept: 
code: 200
Request #1: GET /personAccept: 
code: 200
Request #1: GET /personAccept: 
code: 200

这是方法&#34; getAll&#34;类#34; PersonModel.java&#34;:

@NamedQuery(name="Person.findAll", query="SELECT p FROM Person p")

        public static List<Person> getAll() {
        EntityManager em = LifeCoachDao.instance.createEntityManager();
        List<Person> list = em.createNamedQuery("Person.findAll", Person.class)
            .getResultList();
        LifeCoachDao.instance.closeConnections(em);
        return list;
    }

如何避免这种循环?如果没有这个循环,我怎么能得到代码?

0 个答案:

没有答案