猿问

使用JAXB解组/编组List <String>

我正在尝试创建一个非常简单的REST服务器。我只是有一个测试方法,它将返回字符串列表。这是代码:



@GET

@Path("/test2")

public List test2(){

    List list=new Vector();

    list.add("a");

    list.add("b");

    return list;

}


它给出以下错误:


严重:Java类型的消息正文编写器,

类java.util.Vector和MIME媒体类型,

找不到应用程序/八位位组流

我希望JAXB对诸如String,Integer等简单类型具有默认设置。我想不是。这是我的想象:



<Strings>

  <String>a</String>

  <String>b</String>

</Strings>

使这种方法最简单的方法是什么?


拉莫斯之舞
浏览 519回答 3
3回答

有只小跳蛙

我使用@LiorH的示例并将其扩展为:@XmlRootElement(name="List")public class JaxbList<T>{&nbsp; &nbsp; protected List<T> list;&nbsp; &nbsp; public JaxbList(){}&nbsp; &nbsp; public JaxbList(List<T> list){&nbsp; &nbsp; &nbsp; &nbsp; this.list=list;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlElement(name="Item")&nbsp; &nbsp; public List<T> getList(){&nbsp; &nbsp; &nbsp; &nbsp; return list;&nbsp; &nbsp; }}注意,它使用泛型,因此您可以将其与String之外的其他类一起使用。现在,应用程序代码很简单:&nbsp; &nbsp; @GET&nbsp; &nbsp; @Path("/test2")&nbsp; &nbsp; public JaxbList test2(){&nbsp; &nbsp; &nbsp; &nbsp; List list=new Vector();&nbsp; &nbsp; &nbsp; &nbsp; list.add("a");&nbsp; &nbsp; &nbsp; &nbsp; list.add("b");&nbsp; &nbsp; &nbsp; &nbsp; return new JaxbList(list);&nbsp; &nbsp; }为什么JAXB包中不存在这个简单的类?有人在其他地方看到类似的东西吗?

Qyouu

@GET@Path("/test2")public Response test2(){&nbsp; &nbsp;List<String> list=new Vector<String>();&nbsp; &nbsp;list.add("a");&nbsp; &nbsp;list.add("b");&nbsp; &nbsp;final GenericEntity<List<String>> entity = new GenericEntity<List<String>>(list) { };&nbsp; &nbsp;return Respon

一只斗牛犬

如果任何人都想为包含多个类的元素的列表编写列表包装器,并且想要根据类类型给出单独的XmlElement名称而无需编写X Wrapper类,则可以使用@XmlMixed注释。这样,JAXB将根据设置的值来命名列表中的项目@XmlRootElement。这样做时,您必须使用以下命令指定可能在列表中的类@XmlSeeAlso例:列表中可能的类@XmlRootElement(name="user")public class User {/*...*/}@XmlRootElement(name="entry")public class LogEntry {/*...*/}包装类@XmlRootElement(name="records")@XmlSeeAlso({User.class, LogEntry.class})public static class JaxbList<T>{&nbsp; &nbsp; protected List<T> records;&nbsp; &nbsp; public JaxbList(){}&nbsp; &nbsp; public JaxbList(List<T> list){&nbsp; &nbsp; &nbsp; &nbsp; this.records=list;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlMixed&nbsp;&nbsp; &nbsp; public List<T> getRecords(){&nbsp; &nbsp; &nbsp; &nbsp; return records;&nbsp; &nbsp; }}例:List l = new List();l.add(new User("userA"));l.add(new LogEntry(new UserB()));XStream xStream = new XStream();String result = xStream.toXML(l);结果:<records>&nbsp; &nbsp; <user>...</user>&nbsp; &nbsp; <entry>...</entry></records>另外,您可以使用@XmlElementRef注解在包装类中直接指定XmlElement名称。@XmlRootElement(name="records")@XmlSeeAlso({User.class, LogEntry.class})public static class JaxbList<T>{&nbsp; &nbsp; protected List<T> records;&nbsp; &nbsp; public JaxbList(){}&nbsp; &nbsp; public JaxbList(List<T> list){&nbsp; &nbsp; &nbsp; &nbsp; this.records=list;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlElementRefs({&nbsp; &nbsp; &nbsp; &nbsp; @XmlElementRef(name="item", type=Object.class),&nbsp; &nbsp; &nbsp; &nbsp; @XmlElementRef(name="user", type=User.class),&nbsp; &nbsp; &nbsp; &nbsp; @XmlElementRef(name="entry", type=LogEntry.class)&nbsp; &nbsp; })&nbsp; &nbsp; public List<T> getRecords(){&nbsp; &nbsp; &nbsp; &nbsp; return records;&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答