未找到 Java 类 java.util.ArrayList...和 ​​MIME 媒体类型 text/xml 的消息正文编写器 [英] A message body writer for Java class java.util.ArrayList...and MIME media type text/xml was not found

查看:26
本文介绍了未找到 Java 类 java.util.ArrayList...和 ​​MIME 媒体类型 text/xml 的消息正文编写器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Jersey 来构建 REST 服务,并希望以 XML 形式返回 Collection.

Im using Jersey to build a REST Service and want to return a Collection<String> as 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());
    }
}

但我的尝试失败,出现以下异常:

but my attempts fail with the following exception:

javax.ws.rs.WebApplicationException:com.sun.jersey.api.MessageException:Java 类 java.util.ArrayList、Java 类型类 java.util.ArrayList 和 MIME 媒体类型 text/的消息正文编写器找不到 xml

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type text/xml was not found

我通过谷歌发现的异常的所有结果都处理返回 text/json 而不是 text/xml 就像我的情况一样.

and all results to that exception I found via google dealt with returning text/json instead of text/xml like in my situation.

谁能帮帮我?我想,如果我使用响应,那将是我在 XML 中的根元素和我的集合中的字符串元素列表..

Can anyone help me? I thought, if I use a Response, that would be my root element in XML and my collection a list of string elements in it..

推荐答案

注意:虽然这个答案有效,anar 的回答更好.

NOTE: Although this answer works, anar's answer is better.

您应该尝试使用 JAXB 注释类来解决您的问题.您可以将方法更改为:

You should try to use a JAXB annotated class to solve your problem. You could change your method to this:

@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 注释类.我使用这个答案中描述的过程为您提供了一个生成的类.以下是它将生成的文档示例:

And then create a JAXB annotated class for your groups. I have included a generated class for you, using the process described in this answer. Here is an example of the documents that it will produce:

<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>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{}group" maxOccurs="unbounded"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/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;
    }

}

这篇关于未找到 Java 类 java.util.ArrayList...和 ​​MIME 媒体类型 text/xml 的消息正文编写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆