【问题标题】:Java Web Services and returning Vector<Vector<Object>>Java Web 服务和返回 Vector<Vector<Object>>
【发布时间】:2011-09-30 10:28:29
【问题描述】:

与我之前的问题略有不同。我想将这些保留为 2 个单独的问题(正如另一个问题已得到回答)。

我有一个网络服务,我想将Vector&lt;Vector&lt;Object&gt;&gt; 或ArrayList&lt;ArrayList&lt;Object&gt;&gt; 传回给客户端。为此,我为此创建了一个简单的 POJO 类(感谢 Abhisek Bose),名为 VectorWS。使用Vector的实现如下:

public class VectorWS {
    private Vector vector;

    public VectorWS() {        
    }

    public VectorWS(Vector v) {
        vector = v;
    }

    public Vector getVector() {
        return vector;
    }

    public void setVector(Vector vector) {
        this.vector = vector;
    }
}

接口实现如下:

@WebService
@SOAPBinding(style = Style.RPC)
public interface ISQLServerConnectionWS {

    @WebMethod    
    VectorWS executeSelectSQL(@WebParam(name = "sqlStatements") String sqlStatements);
}

服务端实现如下:

@WebService(endpointInterface="WebServices.ISQLServerConnectionWS")
public class SQLConnectionWSServer
    implements ISQLServerConnectionWS {

    @Override
    public VectorWS executeSelectSQL(String sqlStatements) {
        System.out.println("You are calling executeSelectSQL with " + sqlStatements );           
        Vector results = new Vector();
        Vector row1 = new Vector();
        Vector row2 = new Vector();
        row1.add( "Name" );
        row2.add( "Asamoah" );
        results.add( row1 );
        results.add( row2 );

        VectorWS vws = new VectorWS(new ArrayList(results));

        return vws;           
    }
}

客户端调用服务端服务方法为:

VectorWS results = server.executeSelectSQL( sqlStatements );

但是,当返回vws 时,服务器会抛出异常。我对Vector 和ArrayList 都试过这个。

此实例中的异常(来自 ArrayList)可以在帖子末尾找到。

我的问题是,如果我们不能返回 ArrayList&lt;ArrayList&lt;Object&gt;&gt;,我们会推荐什么? 唯一想到的就是ArrayList&lt;Object[]&gt;。有什么想法吗?

非常感谢,

安德斯

这是异常堆栈跟踪:

javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException
 - with linked exception:
[javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.]
at com.sun.xml.internal.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:318)
at com.sun.xml.internal.ws.message.AbstractMessageImpl.writeTo(AbstractMessageImpl.java:131)
at com.sun.xml.internal.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:98)
at com.sun.xml.internal.ws.encoding.SOAPBindingCodec.encode(SOAPBindingCodec.java:249)
at com.sun.xml.internal.ws.transport.http.HttpAdapter.encodePacket(HttpAdapter.java:328)
at com.sun.xml.internal.ws.transport.http.HttpAdapter.access$100(HttpAdapter.java:82)
at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:470)
at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:233)
at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(WSHttpHandler.java:95)
at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(WSHttpHandler.java:80)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:65)
at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:65)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:68)
at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:555)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:65)
at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:527)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: javax.xml.bind.MarshalException
 - with linked exception:
[javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:268)
at com.sun.xml.internal.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:89)
at com.sun.xml.internal.bind.api.Bridge.marshal(Bridge.java:130)
at com.sun.xml.internal.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:310)
... 18 more
Caused by: javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:234)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:249)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:641)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:54)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:157)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:141)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:321)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:687)
at com.sun.xml.internal.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:136)
at com.sun.xml.internal.bind.v2.runtime.CompositeStructureBeanInfo.serializeBody(CompositeStructureBeanInfo.java:96)
at com.sun.xml.internal.bind.v2.runtime.CompositeStructureBeanInfo.serializeBody(CompositeStructureBeanInfo.java:44)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:687)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:263)
... 21 more
Caused by: javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:554)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:636)
... 31 more

【问题讨论】:

标签: java web-services vector arraylist


【解决方案1】:

您是否尝试过序列化对象,然后在序列化上运行 Base64,通过线路返回,然后解码 base64 并在收到时反序列化?

基本上,您的方法现在将返回一个字符串,即 List/Vectors 的 Base64 编码序列化,接收方需要对字符串进行解编码并解码对象。

我已经使用这种技术来序列化和反序列化复杂的列表和向量对象。

【讨论】:

  • 这对我 user756212 有用。得到它的工作谢谢。将在单独的答案上发布序列化/反序列化代码。
【解决方案2】:

简而言之,根据 user756212 的回答,VectorWS 可以通过以下方式进行序列化/反序列化:

public String serialize(Object o) {
        String s;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream( stream ) );

        encoder.writeObject(o);
        encoder.close();
        s = stream.toString();
        return s;
    }

private Object deserialize(String s) {

    XMLDecoder decoder = new XMLDecoder( new ByteArrayInputStream( s.getBytes() ) );
    return decoder.readObject();
}

并调用:

VectorWS vws = (VectorWS) deserialize( results );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多