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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15493847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:47:34  来源:igfitidea点击:

jersey.api.MessageException message body writer and MIME media type text/xml not found

javaweb-servicesjaxbjersey

提问by Marwan Tushyeh

The data is an object named "QueryResponse" which in turn owns a list of objects called "Todos".

数据是一个名为“QueryResponse”的对象,它又拥有一个名为“Todos”的对象列表。

I get this error:

我收到此错误:

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

I have this jersey get method:

我有这个球衣获取方法:

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

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

And that's the QueryResponse object:

这就是 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;
}
}

And this is the Todo class:

这是 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;
 }
}

I appreciate your help.

我感谢您的帮助。

采纳答案by Amir Kost

Do the following:

请执行下列操作:

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