【发布时间】:2015-08-21 08:15:54
【问题描述】:
我有一个带有 RestRessource 类别的简单 Rest 示例(取自 Jersey Maven Archetype)
@Path("category")
@Produces(MediaType.APPLICATION_JSON)
public class CategoryRessource {
CategoryService service = new CategoryService();
@GET
public List<Category> getCategories() throws SQLException{
List<Category> categories = (ArrayList<Category>) service.getAllCategories();
return categories;
}
有了这个一切正常。
但现在我想将响应类型更改为如下所示的响应,但如果我访问它,则会出现此错误:
09:30:04,688 SEVERE [org.glassfish.jersey.message.internal.WriterInterceptorExecutor] (default task-1) MessageBodyWriter
not found for media type=text/html, type=class java.util.ArrayList, genericType=class java.util.ArrayList.
具有返回类型响应的资源类:
@Path("category")
@Produces(MediaType.APPLICATION_JSON)
public class CategoryRessource {
CategoryService service = new CategoryService();
@GET
public Response getCategories() throws SQLException{
List<Category> categories = (ArrayList<Category>) service.getAllCategories();
return Response .status(Status.OK)
.entity(categories)
.type(MediaType.APPLICATION_JSON)
.build();
}
这也是我的类别模型类
@XmlRootElement
public class Category {
private String name;
public Category(){
}
public String getName() {
return id;
}
public void setName(String name) {
this.name = name;
}
【问题讨论】: