如何在Jersey REST webservice中返回Array? [英] How to return Array in Jersey REST webservice?
问题描述
我是REST webservice的新手,我尝试使用Jersey实现并编写了一个简单的Web服务代码,将List返回给调用客户端:
I am new to REST webservice, I tried using Jersey implementation and wrote a simple webservice code to return List to calling client:
@GET
@Produces(MediaType.TEXT_XML)
public GenericEntity<List<String>> stringlist() {
List<String> list = Arrays.asList("test", "as");
return new GenericEntity<List<String>>(list) {
};
}
我不知道如何在我的客户端获取列表的值。我刚试过在我的客户端使用下面的代码,但是我收到了错误。
I am not sure how to get the value of the list in my client. I just tried using the below code in my client but I am getting error.
service.path("rest")
.path("getVal")
.accept(MediaType.TEXT_XML)
.get(GenericEntity.class
有人可以通过简单的Web服务代码帮助我将Array传递给客户端吗?
Can someone help me with a simple webservice code which passes the Array to client?
推荐答案
您应该只能返回一些@XmlRootElement注释对象的List并访问它们:
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>>{});
由于某些原因,这对于普通字符串来说更复杂,你需要用JAXBElement封装它们
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")
};);
}
并像以前的情况一样访问它,但你需要询问for
And access it similarly as in previous case, but you would need to "ask" for
new GenericEntity<List<JAXBElement<String>>>{}
这篇关于如何在Jersey REST webservice中返回Array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!