gpt4 book ai didi

java - 查询 Jersey REST 服务时发生消息异常 (com.sun.jersey.api.MessageException)

转载 作者:行者123 更新时间:2023-11-30 04:36:31 26 4
gpt4 key购买 nike

上下文: -- 鉴于以下代码,我遇到了异常。请告诉我为什么会发生这种情况,并给出明确的解释:

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

@XmlRootElement
public class Employee{

}

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

Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type interface java.util.List, and MIME media type application/xml was not found ... 30 more

谢谢

最佳答案

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

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

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/

关于java - 查询 Jersey REST 服务时发生消息异常 (com.sun.jersey.api.MessageException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13397796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com