2017-04-12 70 views
0

我有2个项目文件夹。一个用于宁静的服务,另一个用于客户。 基本上,我的客户将调用服务并从特定用户的数据库中获取所有笔记。 (这些注释将显示在表格中)。从RESTful服务检索ArrayList到Jersey客户端

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

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调用代码abov E:

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)); 
} 

我也曾经在两个项目中注对象(注类):

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; 
    } 

} 

回答

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>. 

我所做的就是在REST风格的代码插入这个(公开回应登录)

@Produces({MediaType.APPLICATION_XML}) 

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

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