【问题标题】:Messaging exception (com.sun.jersey.api.MessageException) while querying Jersey REST service查询 Jersey REST 服务时出现消息异常 (com.sun.jersey.api.MessageException)
【发布时间】:2012-11-27 18:38:37
【问题描述】:

上下文:- 鉴于以下代码,我得到了异常。请用明确的解释告诉我为什么会这样:

@GET
@Produces("application/xml")
public List getEmployee()
{
   List<Employee> emp=new ArrayList<Employee>();
   return emp;
}

@XmlRootElement
public class Employee{

}

当我调用 getEmployee 服务时,出现以下异常:

原因:com.sun.jersey.api.MessageException:消息正文编写器 用于 Java 类 java.util.ArrayList 和 Java 类型接口 java.util.List 和 MIME 媒体类型 application/xml 未找到 ... 30 更多

谢谢

【问题讨论】:

  • 将泛型参数类型添加到您的 List 返回类型中。

标签: java web-services rest


【解决方案1】:

您正在重新调整员工列表,该列表是 ArrayList 的一个实例。您在 Employee 类上声明了根注释,而不是在 arraylist 上。

您需要创建一个包装器来保存员工列表。这个包装器将使您能够为列表创建根元素,即

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "users")
public class Users {

    @XmlElement(name="user")
    private ArrayList users;

    public ArrayList getUsers() {
        return users;
    }

    public void setUsers(ArrayList users) {
        this.users = users;
    }
}

请参考以下教程了解更多

http://howtodoinjava.com/2012/11/26/writing-restful-webservices-with-hateoas-using-jax-rs-and-jaxb-in-java/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2014-02-17
    • 2013-03-25
    • 2019-11-06
    • 1970-01-01
    • 2019-02-09
    • 2018-05-12
    相关资源
    最近更新 更多