在开发使用JAX风格的XML的服务器/客户端应用程序时,我发现,每当我在JAX上使用LinkedList<T>
作为参数或返回类型时,上述LinkedList<T>
的内容就会消失。
我写了一个例子程序来证明这个问题。在这个应用程序中,创建了一个新的服务器"Vegeta“,客户端"Nappa”可以问它一个问题。每个句子都是一个LinkedList<Word>
,通过JAX在它们之间传递.这些LinkedList<Word>
对象总是空的到达另一边。
,有人知道为什么会这样吗?
如果有帮助,我的演示代码如下所示。任何和所有的答案都将不胜感激。
接口IServer是Nappa用来与Vegeta对话的接口:
package com.stuffvegetasays;
import java.util.LinkedList;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public interface IServer
{
@WebMethod abstract LinkedList<Word> answer(LinkedList<Word> question);
}
类Vegeta实现IServer (以及作为服务器的行为)将自身传递给Nappa,以便Nappa可以使用RPC访问它的方法
package com.stuffvegetasays;
import java.util.LinkedList;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService(endpointInterface = "com.stuffvegetasays.IServer")
public class Vegeta implements IServer
{
public static void main(String[] args)
{
Vegeta vegeta = new Vegeta();
System.out.println("Starting Server");
String serverAddress = "http://0.0.0.0:9000/Vegeta";
Endpoint serverEnd = Endpoint.create(vegeta);
serverEnd.publish(serverAddress);
System.out.println("Server Published!");
}
@Override
public LinkedList<Word> answer(LinkedList<Word> question)
{
System.out.println(question);
LinkedList<Word> answer = new LinkedList<Word>();
answer.add(new Word("\n\n"));
answer.add(new Word("It's "));
answer.add(new Word("over "));
answer.add(new Word("NINE "));
answer.add(new Word("THOUSAND!!!"));
return answer;
}
}
类Nappa充当客户端,调用Vegeta的URL。
package com.stuffvegetasays;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class Nappa
{
// Do an RPC style call to the published server "Vegeta".
public static void main(String[] args) {
URL serverURL;
try
{
serverURL = new URL("http://localhost:9000/Vegeta/");
QName appServerQName = new QName("http://stuffvegetasays.com/", "VegetaService");
Service service = Service.create(serverURL, appServerQName);
IServer vegeta = service.getPort(IServer.class);
LinkedList<Word> question = new LinkedList<Word>();
question.add(new Word("Vegeta! "));
question.add(new Word("What "));
question.add(new Word("does "));
question.add(new Word("the "));
question.add(new Word("scouter "));
question.add(new Word("say "));
question.add(new Word("about "));
question.add(new Word("his "));
question.add(new Word("power "));
question.add(new Word("level?"));
System.out.println(question);
LinkedList<Word> quote = vegeta.answer(question);
System.out.println(quote);
}
catch (MalformedURLException e)
{
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
类Word只是一个带有修改后的toString()方法的字符串的容器。
package com.stuffvegetasays;
public class Word
{
String value;
public Word(String value)
{
this.value = value;
}
public String toString()
{
return value;
}
}
发布于 2013-04-19 06:58:24
避免直接返回集合。而不是返回一个
List<X>
,而是返回一个包含List<X>
的JavaBean。这将为您提供更详细的控制编组无论如何。
http://shrubbery.mynetgear.net/c/display/W/Producing+a+Web+Service+with+JAX-WS+and+JAXB
另请参阅:
https://stackoverflow.com/questions/16096996
复制相似问题