从RESTful服务检索ArrayList到Jersey客户端

时间:2017-04-12 23:42:02

标签: java web-services rest jersey jersey-client

我有2个项目文件夹。一个用于休息服务,一个用于客户端。 基本上我的客户端将调用该服务并从特定用户的DB获取所有注释。 (这些注释将显示在表格中)。

这是我得到的错误(现在):

org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
    SEVERE: MessageBodyWriter not found for media type=text/html, type=class java.util.ArrayList, genericType=java.util.List<inventory.Note>.

和这个

com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class java.util.List, and Java type java.util.List<Model.Note>, and MIME media type text/html; charset=utf-8 was not found.

在我的restful服务中,我有以下代码,它应该返回一个带有基于用户名的所有笔记的arraylist:

 @Path("/getAll")
@POST
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
public Response login(@FormParam("username") String uname) throws ClassNotFoundException, SQLException{
    .
    .
    List<Note> note_AL = new ArrayList<Note>();
    .
    .
    (Insert stuf into the array list)
    .
    .
    GenericEntity<List<Note>> generic_list_of_notes = new GenericEntity<List<Note>>(note_AL){};

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

然后在我的客户端项目中,我有一个调用上面代码的servlet:

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Form form = new Form();
    form.add("username", "mike");

    Client client = Client.create();

    WebResource webR = client.resource("http://localhost:8080/MyNote/api/notes/getAll");

    ClientResponse resp = webR.accept("text/html").post(ClientResponse.class, form);

    //This get printed out (so maybe I have an extra error?
    if (resp.getStatus() != 200){
             System.err.println("Unable to connect to the RESTFUL web service");
    }

    //I get the error here
    ArrayList<Model.Note> output = (ArrayList<Model.Note>) resp.getEntity(new GenericType<List<Model.Note>>(){});

    //Don't know if this is correct. Haven't reached this step because of the above error:
    request.setAttribute("note-all", resp.getEntity(ArrayList.class));
}

我在两个项目中都有一个Note对象(Note类):

public class Note {
    private int id;
    private String title;
    private String text;
    private String color;
    private String date;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }


    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }


    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }


    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }


    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }

}

1 个答案:

答案 0 :(得分:0)

我已经解决了。

org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
    SEVERE: MessageBodyWriter not found for media type=text/html, type=class java.util.ArrayList, genericType=java.util.List<inventory.Note>.

我所做的是将其插入RESTful代码(公共响应登录)

@Produces({MediaType.APPLICATION_XML})

然后在我的客户端servlet中更改为以下行:

ClientResponse resp = webR.accept(MediaType.APPLICATION_XML/*"text/html"*/).post(ClientResponse.class, form);