使用Java JAX-WS返回字符串数组? [英] Using Java JAX-WS To Return a String Array?
问题描述
我试图创建一个简单的Web服务,该Web服务在被调用时应该返回一个String数组,但似乎没有这样做,而且我不知道为什么吗?
I have attempted to create a simple web service that when called is supposed to return a String array, yet it does not seem to do so and i cannot figure out why?
此Web服务如下:
@WebService
public String[] getData(String testParam)
{
String[] testArray= null;
....//DOES SOMETHING TO TEST PARAM//.....
testArray= longtestString.split("-");
return testArray;
}
现在,我创建了一个尝试调用此函数并收到String数组的客户端,该客户端的代码为:
Now i create a client that attempts to call this function and recieve a String array back, the code for the client is:
myjaxpackage.GetDataService service = new myjaxpackage.GetDataService();
myjaxpackage.GetData port = service.getGetDataPort();
String testParameter= "this-is-a-test-string";
String[] result = port.getData(testParameter); //Incompatible Types???
然而,ide(Netbeans)给我错误,指出它们是不兼容的类型:
Yet the ide (Netbeans) is giving me the error stating that they are incompatible types:
incompatible types
required: java.lang.String[]
found: java.util.List<java.lang.String>
有人会知道为什么这段代码不正确吗?该函数应该返回一个字符串数组,并且该函数将被调用并送入一个数组中,所以我看不到它们怎么可能不兼容?
Would anyone know why this code is not correct, the function is supposed to return a string array and it is being called and fed into one so i dont see how they can be incompatible?
感谢您对此事的帮助.
推荐答案
此处使用的默认绑定是JAXB-序列的默认映射是List,因此在解组时,JAXB将响应的序列转换为列表与String []不兼容.您有两种解决方法:
The default binding used here is JAXB - the default mapping of a sequence is a List, so when unmarshalling, JAXB is transforming the sequence in response, to a list which is not compatible with String[]. You have two workarounds:
-
要将签名更改为与JAXB兼容的某种东西,即
List<String>
使用XMLJavaTypeAdapter
表示要修改类型-在返回之前将列表转换为数组-请参见
Use XMLJavaTypeAdapter
to indicate you would like the types to be adapted - to convert the List to Array before returning - see link to javadoc
这篇关于使用Java JAX-WS返回字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!