@GET
@Produces("application/json")
public Response GetAll() throws JSONException{
tblCategoryDao dao = new tblCategoryDao();
List<tblcategory> categories= dao.getAllCategories();
JSONArray JSONArray = new JSONArray(categories);
return Response.status(200).entity(JSONArray).build();
}
以上是我的代码,用于返回类别列表的json。 我正在使用JSONArray将列表转换为json字符串。 我收到以下错误
SEVERE: A message body writer for Java class org.json.JSONArray, and Java type class org.json.JSONArray, and MIME media type application/json was not found.
The registered message body writers compatible with the MIME media type are:
application/json ->
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
实际问题是什么?
答案 0 :(得分:2)
以下是解决方案:
return Response.status(200).entity(JSONArray.toString()).build();
您必须将.toString()
添加到实体数据中。