Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 查询jersey REST服务时出现消息传递异常(com.sun.jersey.api.MessageException)_Java_Web Services_Rest - Fatal编程技术网

Java 查询jersey REST服务时出现消息传递异常(com.sun.jersey.api.MessageException)

Java 查询jersey REST服务时出现消息传递异常(com.sun.jersey.api.MessageException),java,web-services,rest,Java,Web Services,Rest,Context:--给定以下代码,我将得到异常。请告诉我为什么会发生这种情况,并给出明确的解释: @GET @Produces("application/xml") public List getEmployee() { List<Employee> emp=new ArrayList<Employee>(); return emp; } @XmlRootElement public class Employee{ } @GET @生成(“应用程序/xm

Context:--给定以下代码,我将得到异常。请告诉我为什么会发生这种情况,并给出明确的解释:

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

@XmlRootElement
public class Employee{

}
@GET
@生成(“应用程序/xml”)
公共列表getEmployee()
{
List emp=new ArrayList();
返回emp;
}
@XmlRootElement
公营雇员{
}
当我调用getEmployee服务时,我遇到以下异常:

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


谢谢

您正在重新调整包含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;
    }
}
请参阅下面的教程以了解更多信息


将通用参数类型添加到列表返回类型中。