简体   繁体   English

Arraylist实施

[英]Arraylist implementation

I have a class Researcher, and an ArrayList named researcherList that contains a list of researchers. 我有一个类Researcher和一个名为researcherList的ArrayList,它包含一个研究人员列表。
I want to get all researchers information from the database and put it in the researcherlist ArrayList that I created, and my response to client will be list of researchers. 我想从数据库中获取所有研究人员的信息并将其放入我创建的研究人员列表中,我对客户的回复将是研究人员名单。 I have written the code below please try to see the code and find the bug. 我已经编写了下面的代码,请尝试查看代码并找到错误。 It is not working. 它不起作用。 It creates an exception. 它创造了一个例外。 Here is the exception 这是例外

SEVERE: A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/xml, was not found
SEVERE: Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException

@GET
@Path("/researcher/all")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getAllResearchers() {

    //TODO return proper representation object

    Researcher res = new Researcher();
    List<Researcher> researcherList = new ArrayList<Researcher>();
    try {
        ResultSet resList = researcher.getAllResearcher();

        while (resList.next()) {
            res.setId(resList.getInt("id"));
            res.setResearcherName(resList.getString("name"));
            res.setAge(resList.getInt("age"));
            res.setSex(resList.getString("sex"));
            res.setCity(resList.getString("city"));
            res.setStreet(resList.getString("street"));
            res.setTelephone(resList.getString("telephone"));
            researcherList.add(res);
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
    }

    return Response.ok(researcherList).build();
}

You are adding the same Researcher object all the time in a loop! 您是在循环中一直添加相同的Researcher对象! Are you sure it is desired? 你确定它是你想要的吗?

I suppose moving Researcher res = new Researcher(); 我想移动Researcher res = new Researcher(); after while (resList.next()) { ..... might come in handy. 之后while (resList.next()) { .....可能会派上用场。

The Response doesn't have a handler for the ArrayList type. Response没有ArrayList类型的处理程序。

I would convert it to a String and return that. 我会将它转换为String并返回它。 As a first cut, you could try: 作为第一次切割,您可以尝试:

return Response.ok(researcherList.toString()).build();

Also, you are adding the same Researcher object every time. 此外,您每次都添加相同的 Researcher对象。 You should create a new Researcher object every loop iteration : 您应该在每次循环迭代时创建一个新的Researcher对象

List<Researcher> researcherList = new ArrayList<Researcher>();
try {
    Researcher res = new Researcher();
    researcherList.add(res);
    ...

Your eventual solution would probably involve building an HTML String from your list: 您的最终解决方案可能涉及从列表中构建HTML字符串:

return Response.ok(convertToHtml(researcherList)).build();

...

private static String convertToHtml(String< Researcher > researcherList) {
    // some implementation
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM