我在控制台上收到以下消息:
`MessageBodyWriter not found for media type=application/json, type=class java.util.HashMap, genericType=class java.util.HashMap.`
调用以下ReST服务:
@GET
@Path("/m1")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, ArrayList<EntityClass>> m1(){
return new Definition().webMethod1();
}
所以我实现了MessageBodyWriter
类,如下所示:
package web;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Map;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class Writer implements MessageBodyWriter<Map<String, ArrayList<EntityClass>>>{
@Override
public long getSize(Map<String, ArrayList<EntityClass>> arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
return arg0.size();
}
@Override
public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
return arg0 == EntityClass.class;
}
@Override
public void writeTo(Map<String, ArrayList<EntityClass>> arg0, Class<?> arg1, Type arg2, Annotation[] arg3,
MediaType arg4, MultivaluedMap<String, Object> arg5, OutputStream arg6)
throws IOException, WebApplicationException {
System.out.println("Exe started..............................................................................");
StringBuffer sb = new StringBuffer("{");
ArrayList<EntityClass> entityObj = arg0.get("Entity");
for(EntityClass eobj: entityObj){
sb.append("[{");
sb.append("\"id\":\""+eobj.getId()+"\",");
sb.append("\"name\":\""+eobj.getName()+"\"");
sb.append("}],");
}
sb.replace(sb.length()-1, sb.length(), "}");
System.out.println(sb);
arg6.write(sb.toString().getBytes());
}
}
我仍然收到同样的错误,并且控制台上没有显示system.out.println
类writeTo
类的writer
行。
可能是什么错误,可能是一个可能的解决方案?