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