How to return Array in Jersey REST webservice?

会有一股神秘感。 提交于 2019-12-07 13:20:26

You should be able to return just List of some @XmlRootElement annotated objects and access them:

service.path("rest").path("getVal").accept(MediaType.TEXT_XML).get(new GenericEntity<List<MyObj>>{});

for some reason this is more complicated with plain strings, you need to encapsulate them with JAXBElement

@GET
@Produces(MediaType.TEXT_XML)
public List<JAXBElement<String>> stringlist() {
     Arrays.asList(new JAXBElement[] {
        new JAXBElement(QName.valueOf("element1"), String.class, "ahoj"),
        new JAXBElement(QName.valueOf("element2"), String.class, "nazdar")
    };);
}

And access it similarly as in previous case, but you would need to "ask" for

new GenericEntity<List<JAXBElement<String>>>{}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!