简体   繁体   English

未找到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

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

but my attempts fail with the following exception: 但我的尝试失败,出现以下异常:

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 javax.ws.rs.WebApplicationException:com.sun.jersey.api.MessageException:Java类java.util.ArrayList的消息体编写器,Java类型类java.util.ArrayList,MIME媒体类型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. 我发现通过google处理的返回text / json而不是text / 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.. 我想,如果我使用Response,那将是我在XML和我的集合中的根元素,其中包含一个字符串元素列表。

Use 使用

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

The Generic entity wrapper works to get the output when using the Response builder. 使用“响应”构建器时,通用实体包装器用于获取输出。

Reference 参考

NOTE: Although this answer works, anar's answer is better. 注意:尽管这个答案有效,但anar的答案更好。

You should try to use a JAXB annotated class to solve your problem. 您应该尝试使用JAXB注释类来解决您的问题。 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());
    }
}

And then create a JAXB annotated class for your groups. 然后为您的组创建一个JAXB注释类。 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>

And here is the generated class: 这是生成的类:

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;
    }

}

The only thing that worked for me so far is to create my own Wrapper object. 到目前为止,唯一对我有用的是创建自己的Wrapper对象。

Don't forget the @XmlRootElement annotation to explain JAXB how to parse it. 不要忘记@XmlRootElement注释来解释JAXB如何解析它。

Note that this will work for any type of object - in this example I used ArrayList of String. 请注意,这适用于任何类型的对象 - 在此示例中,我使用String的ArrayList。

eg 例如

The Wrapper object should look like this: Wrapper对象应如下所示:

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class ArrayListWrapper {
    public ArrayList<String> myArray = new ArrayList<String>();
}

And the REST method should look like this: 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.

相关问题 找不到Java类型类java.util.ArrayList和MIME媒体类型application / xml的消息正文编写器 - A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/xml, was not found 严重:未找到 Java 类 java.util.ArrayList 和 MIME 媒体类型 application/json 的消息正文编写器 - SEVERE: A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found 找不到Java类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器 - A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found 找不到Java类java.util.ArrayList和Java类型类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器 - A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/json was not found Java类java.util.ArrayList的消息体编写器 - A message body writer for Java class java.util.ArrayList 找不到用于Java类型,类bookInfoListType和MIME媒体类型application / xml的消息正文编写器 - A message body writer for Java type, class bookInfoListType, and MIME media type application/xml was not found 找不到用于Java类型myPackage.Sample和MIME媒体类型application / xml的消息正文编写器 - A message body writer for Java type, class myPackage.Sample, and MIME media type, application/xml, was not found 获取错误Java类java.util.ArrayList / List的消息正文编写器 <java.lang.String> 没有找到 - Getting error A message body writer for Java class java.util.ArrayList/List<java.lang.String> was not found 未找到 Media type=text/plain、type=class java.util.ArrayList、genericType=java.util.List 的 MessageBodyWriter<models.Person> - MessageBodyWriter not found for media type=text/plain, type=class java.util.ArrayList, genericType=java.util.List<models.Person> 我看到错误:Java类java.util.ArrayList和Java类型java.util.List的消息正文编写器<java.lang.String> - I am seeing the error: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<java.lang.String>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM