Reputation: 488
Iam learning REST webservice. I have written a very basic code to return a List from the webservice. below is code snippet
@Path("hello")
public class Hello {
@GET
@Produces(MediaType.TEXT_PLAIN)
public List<String> greeting() {
List<String> greeting = new ArrayList<>();
greeting.add("Hello World");
greeting.add("How are you");
greeting.add("Hope you are doing good");
greeting.add("Hey WhatsApp");
greeting.add("Take care");
greeting.add("Perform well");
return greeting;
}
}
the messagebodywriter implementation is below
@Provider
@Produces(MediaType.TEXT_PLAIN)
public class ListMessageBodyWriter implements MessageBodyWriter<List<String>>{
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
System.out.println("here in the isWriteable");
return type == List.class;
}
@Override
public void writeTo(List<String> t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
System.out.println("here in the writeTo");
System.out.println("t="+t);
System.out.println("size of t "+t.size());
Writer writer = new PrintWriter(entityStream);
writer.write("list of string will be returned later");
writer.flush();
writer.close();
}
But when i run the code i still get the same error as below
MessageBodyWriter not found for media type=text/plain, type=class java.util.ArrayList, genericType=java.util.List.
Why iam getting the same error despite implementing the messagebodywriter?
Upvotes: 0
Views: 186
Reputation: 488
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
System.out.println("here in the isWriteable");
return type == List.class;
}
changing the return type == List.class; to
return type == ArrayList.class; solved the error.
Upvotes: 1