
[英]A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/xml, was not found
[英]A message body writer for Java class java.util.ArrayList…and MIME media type text/xml was not found
我使用Jersey构建REST服务,并希望将Collection<String>
作为XML返回。
@GET
@Produces(MediaType.TEXT_XML)
@Path("/directgroups")
public Response getDirectGroupsForUser(@PathParam("userId") String userId) {
try {
Collection<String> result = service.getDirectGroupsForUser(userId, null, true);
// return result; //first try
// return result.toArray(new String[0]); //second try
return Response.ok().type(MediaType.TEXT_XML).entity(result).build(); //third try
} catch (UserServiceException e) {
LOGGER.error(e);
throw new RuntimeException(e.getMessage());
}
}
但我的尝试失败,出现以下异常:
javax.ws.rs.WebApplicationException:com.sun.jersey.api.MessageException:Java类java.util.ArrayList的消息体编写器,Java类型类java.util.ArrayList,MIME媒体类型text / xml不是发现
我发现通过google处理的返回text / json而不是text / xml就像我的情况一样。
谁能帮我? 我想,如果我使用Response,那将是我在XML和我的集合中的根元素,其中包含一个字符串元素列表。
使用
List<String> list = new ArrayList<String>();
GenericEntity<List<String>> entity = new GenericEntity<List<String>>(list) {};
Response response = Response.ok(entity).build();
使用“响应”构建器时,通用实体包装器用于获取输出。
注意:尽管这个答案有效,但anar的答案更好。
您应该尝试使用JAXB注释类来解决您的问题。 您可以将方法更改为:
@GET
@Produces(MediaType.TEXT_XML)
@Path("/directgroups")
public Groups getDirectGroupsForUser(@PathParam("userId") String userId) {
try {
Groups groups = new Groups();
groups.getGroup().addAll(service.getDirectGroupsForUser(userId, null, true));
return groups;
} catch (UserServiceException e) {
LOGGER.error(e);
throw new RuntimeException(e.getMessage());
}
}
然后为您的组创建一个JAXB注释类。 我已经使用本答案中描述的过程为您包含了一个生成的类。 以下是它将生成的文档示例:
<groups>
<group>Group1</group>
</group>Group2</group>
</groups>
这是生成的类:
package example;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element ref="{}group" maxOccurs="unbounded"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"group"
})
@XmlRootElement(name = "groups")
public class Groups {
@XmlElement(required = true)
protected List<String> group;
/**
* Gets the value of the group property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the group property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getGroup().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
*
*
*/
public List<String> getGroup() {
if (group == null) {
group = new ArrayList<String>();
}
return this.group;
}
}
到目前为止,唯一对我有用的是创建自己的Wrapper对象。
不要忘记@XmlRootElement注释来解释JAXB如何解析它。
请注意,这适用于任何类型的对象 - 在此示例中,我使用String的ArrayList。
例如
Wrapper对象应如下所示:
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class ArrayListWrapper {
public ArrayList<String> myArray = new ArrayList<String>();
}
REST方法应如下所示:
@GET
@Produces(MediaType.TEXT_XML)
@Path("/directgroups")
public ArrayListWrapper getDirectGroupsForUser(@PathParam("userId") String userId) {
try {
ArrayListWrapper w = new ArrayListWrapper();
w.myArray = service.getDirectGroupsForUser(userId, null, true);
return w;
} catch (UserServiceException e) {
LOGGER.error(e);
throw new RuntimeException(e.getMessage());
}
}
将@XmlRootElement(name =“class name”)添加到我想要返回的对象,解决了我的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.