1

在开发使用 JAX-WS RPC 样式 XML 的服务器/客户端应用程序期间,我发现每当我LinkedList<T>在 JAX-WS 上使用 a 作为参数或返回类型时,say 的内容LinkedList<T>都会消失。

我写了一个示例程序来演示这个问题。在这个应用程序中,创建了一个新的服务器“Vegeta”,客户端“Nappa”可以向它提问。每个都是 a 的句子LinkedList<Word>通过 JAX-WS RPC 在它们之间传递。这些LinkedList<Word>物品总是空着到达另一边。

有谁知道为什么会这样?

如果有帮助,我的演示代码如下。任何和所有的答案将不胜感激。

Interface 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;
    }
}
4

1 回答 1

1

避免直接返回集合。与其返回 a List<X>,不如返回一个包含 a 的 JavaBean List<X>。无论如何,这将使您对编组进行更详细的控制。

使用 JAX-WS 和 JAXB 生成 Web 服务

也可以看看:

于 2013-04-19T06:58:24.017 回答