0

I have a class Researcher, and an ArrayList named researcherList that contains a list of researchers.
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();
}
home
  • 12,468
  • 5
  • 46
  • 54
user1046350
  • 21
  • 1
  • 4
  • 1
    **What** exception is raised, and on what line? – Mat Nov 20 '11 at 19:57
  • 4
    "It is not working. It creates an exception." That means you've got information which you haven't told us - what the exception is. Please read http://tinyurl.com/so-hints. Oh, and just *swallowing* exceptions and continuing as if they never happened (as you are in your code above with SQLException) is almost always a bad idea. – Jon Skeet Nov 20 '11 at 19:58
  • 1
    When you have gotten past the exception part you still have a problem. You only create one researcher which you set all data on and finally add to the list. Then you modify the same researcher and re-add it to the list. When done you will have n references to the SAME researcher (last updated data) in the list. This is probably not what you want. – Roger Lindsjö Nov 20 '11 at 20:03
  • Did you try to just google the exception `A message body writer...` - first [hit is SO](http://stackoverflow.com/questions/1603404/using-jaxb-to-unmarshal-marshal-a-liststring)? – home Nov 20 '11 at 20:14

2 Answers2

1

You are adding the same Researcher object all the time in a loop! Are you sure it is desired?

I suppose moving Researcher res = new Researcher(); after while (resList.next()) { ..... might come in handy.

Wojciech Owczarczyk
  • 5,595
  • 2
  • 33
  • 55
1

The Response doesn't have a handler for the ArrayList type.

I would convert it to a String and return that. As a first cut, you could try:

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

Also, you are adding the same Researcher object every time. You should create a new Researcher object every loop iteration:

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:

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

...

private static String convertToHtml(String< Researcher > researcherList) {
    // some implementation
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722