Java 类 java.util.ArrayList 的消息正文编写器...并且找不到 MIME 媒体类型文本/xml

2022-09-02 05:13:25

我使用泽西岛来构建 REST 服务,并希望返回一个作为 XML。Collection<String>

@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

我通过谷歌找到的那个例外的所有结果都处理了返回文本/ json而不是文本 / xml,就像我的情况一样。

任何人都可以帮我吗?我想,如果我使用响应,那将是我在XML中的根元素,而我的集合是其中的字符串元素列表。


答案 1

List<String> list = new ArrayList<String>();
GenericEntity<List<String>> entity = new GenericEntity<List<String>>(list) {};
Response response = Response.ok(entity).build();

通用实体包装器用于在使用响应生成器时获取输出。

参考


答案 2

注意:虽然这个答案有效,但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>
 * &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;
    }

}