问题描述
我有以下要返回的Web服务和数组值
I have the following web service in which I want to return and values of array
@GET
@Consumes("text/plain")
@Produces("text/plain")
public String[] getText(@PathParam("name") String Uname) {
//TODO return proper representation object
System.out.println("In Method " + Uname);
String arr[]=null;
arr=new String[2];
arr[0]="demo";
arr[1]="demo2";
return arr;
}
但是当我测试此Web服务时,它给了我这个错误:GET RequestFailed RequestFailed --> Status: (406) Response: {
But when I test this web services it is giving me this error: GET RequestFailed RequestFailed --> Status: (406) Response: {
如果我想从REST Web服务返回数组怎么办?
So what should I do if I want to return an array from a REST webservice?
推荐答案
HTTP响应不支持纯文本响应中的数组.您要么需要手动将数组表示为String,然后将返回类型更改为String并返回,如下所示:
HTTP responses do not support Arrays in plain text responses. You either need to manually represent your array as a String, change the return type to String and return like this:
return Arrays.toString(arr);
或者您可以将数组转换为列表: return Arrays.asList(arr);
Or you could convert your array to a List: return Arrays.asList(arr);
,并在此处使用将其返回为JSON或XML的方法:泽西岛:返回字符串列表
and use the approach to return it as JSON or XML here:Jersey: Return a list of strings
这篇关于如何从Restful Web服务返回数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!