jersey.api.MessageException消息正文编写器和MIME媒体类型text / xml未找到

时间:2013-03-19 07:35:37

标签: java web-services jaxb jersey

数据是名为" QueryResponse"的对象。而这又拥有一个名为" Todos"。

的对象列表

我收到此错误:

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message  body writer for Java class java.util.ArrayList, and Java type java.util.List<de.vogella.jersey.todo.model.Todo>, and MIME media type text/xml was not found

我有这种球衣获取方法:

@GET
@Produces({"application/xml", "application/json"})
public QueryResponse getTodos() {

 List todos = new ArrayList();
 todos.addAll(TodoDao.instance.getModel().values());
 return  new QueryResponse(todos);
}

这就是QueryResponse对象:

@XmlRootElement

public class QueryResponse {
@XmlElementWrapper(name = "Todos")
@XmlElement(name = "Todo")
private List<Todo> todolist;
public QueryResponse(List<Todo> todolist)
{
    this.todolist = todolist;
}

public void setTodolist(List<Todo> todolist)
{
    this.todolist = todolist;
}
public List<Todo> getTodolist( )
{
    return this.todolist;
}
}

这是Todo课程:

public class Todo
{


  private int id;
  private String summary;
  private String Description;

  public Todo()
 {
 }

  public Todo(int id, String summary)
 {
   this.id = id;
   this.summary = summary;
 }
 public int getId() {
   return this.id;
 }
 public void setId(int userID) {
   this.id = userID;
 }
 public String getSummary() {
   return this.summary;
 }
 public void setSummary(String summary) {
   this.summary = summary;
 }
 public String getDescription() {
   return this.Description;
 }
 public void setDescription(String description) {
    this.Description = description;
 }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

执行以下操作:

  1. 使用以下注释对public void getTodolist(List<Todo> todolist)进行注释:@XmlElementRef
  2. 使用以下注释注释您的QueryResponse:@XmlSeeAlso({Todo.class})