如何使用Java中的Web服务(例如Axis2)发送复杂对象的数组或集合? [英] How do I send an array or collection of complex objects using web services in Java (e.g. Axis2)?

查看:217
本文介绍了如何使用Java中的Web服务(例如Axis2)发送复杂对象的数组或集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对SOAP / web服务比较陌生;虽然我做了一些较小的Web服务项目,我偶然没有需要返回(或使用作为参数)一个数组或复杂对象集合。



当我使用 RPC / literal 时,我可以发送和接收内置类型的数组(例如String []),但是当我尝试一个'复杂'对象时,我得到了封送错误(例如,xyz或其任何超类在这个上下文中是已知的)



或者,尝试文档/文字/包装(这似乎是最佳实践?)允许我使用@XmlSeeAlso注释。发送或接收复杂的对象,但是当我尝试传递任何类型的数组时会导致一些怪事。例如, String [] t = {A,B,C,D}; 字符串(不是 String [] )。



有没有人知道这里发生了什么,任何明智的方式?我现在正在本地部署到Apache Geronimo。



服务器代码段:



接口:

  @WebService 
@SOAPBinding(style = Style.DOCUMENT ,use = Use.LITERAL,parameterStyle = ParameterStyle.WRAPPED)
@XmlSeeAlso({Feedback.class,Feedback []。class})
public interface CerberusRequest {



@WebMethod
public Feedback [] test(String [] facts)throws Exception;

}

实施:

  @WebService(serviceName =CerberusRequestService,portName 
=CerberusRequestPort,targetNamespace =http://web.cerberus .bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb test(String [] facts)throws Exception {
//只是一些debug crap(打印:in test ... 1 [Ljava.lang.String;)
System.out.println in test ...+ facts.length ++ facts.getClass()。getName());
for(String f:facts){
System.out.println(test:+ f);
}
// return feedback;
反馈[] f = {new Feedback()};
return f;
}
}

客户程式码片段

  URL url = new URL(destination +?wsdl); 
QName qname = new QName(namespace,service);

服务service = Service.create(url,qname);
CerberusRequest sr = service.getPort(CerberusRequest.class);

String [] t = {A,B,C,D};
Feedback [] feedback = sr.test(t);
for(Feedback f:feedback){
System.out.println(f);
}

WSDL:

 <?xml version =1.0encoding =utf-8?> 
< definitions xmlns =http://schemas.xmlsoap.org/wsdl/xmlns:soap =http://schemas.xmlsoap.org/wsdl/soap/xmlns:tns =http: //web.cerberus.xyz.gov/xmlns:xsd =http://www.w3.org/2001/XMLSchemaname =CerberusRequestServicetargetNamespace =http://web.cerberus.xyz.gov/ >
< types>
< xsd:schema>
< xsd:import namespace =http://web.cerberus.xyz.gov/schemaLocation =http:// localhost:8080 / CerberusService / CerberusRequest?xsd = xsd1/>
< / xsd:schema>
< xsd:schema>
< xsd:import namespace =http://jaxb.dev.java.net/arrayschemaLocation =http:// localhost:8080 / CerberusService / CerberusRequest?xsd = xsd2/>
< / xsd:schema>
< / types>

< message name =Exception>
< part element =tns:Exceptionname =fault>
< / part>
< / message>
< message name =test>
< part element =tns:testname =parameters>
< / part>
< / message>
< message name =testResponse>

< part element =tns:testResponsename =parameters>
< / part>
< / message>
< portType name =CerberusRequest>
< operation name =test>
< input message =tns:test>
< / input>
< output message =tns:testResponse>
< / output>

< fault message =tns:Exceptionname =Exception>
< / fault>
< / operation>
< / portType>
< binding name =CerberusRequestPortBindingtype =tns:CerberusRequest>
< soap:binding style =documenttransport =http://schemas.xmlsoap.org/soap/http/>
< operation name =test>
< soap:operation soapAction =/>
< input>

< soap:body use =literal/>
< / input>
< output>
< soap:body use =literal/>
< / output>
< fault name =Exception>
< soap:fault name =Exceptionuse =literal/>
< / fault>
< / operation>

< / binding>
< service name =CerberusRequestService>
< port binding =tns:CerberusRequestPortBindingname =CerberusRequestPort>
< soap:address location =http:// localhost:8080 / CerberusService / CerberusRequest/>
< / port>
< / service>
< / definitions>

非常感谢您提供的任何和所有帮助!

$ b好吧,我有类似的问题,我想通过VO,它有一个复杂的对象(地图)里面,这个地图可以包含地图的地图作为值或地图列表。



即使你支持当前的要求,你可能最终会在其他问题后。更好地,你可以使用第三方序列化器,如Castor / Dozer / GSon这样的工具。并更改数据一个简单的java类型,可以很容易通过线传递。


I'm relatively new to SOAP/web services; while I've done a few smaller web services projects I've incidentally never needed to return (or use as a parameter) an array or collection of 'complex' objects. When I attempt to do so, I get varying odd behaviour depending on my SOAP binding style.

When I use RPC/literal, I can send and receive arrays of built-in types (e.g. String[]), but when I try a 'complex' object, I get marshaling errors (e.g. xyz nor any of its super class is known to this context), despite having added the relevant classes to the @XmlSeeAlso annotation.

Alternatively, trying Document/literal/wrapped (which seems to be best practice?) allows me to send or receive complex objects, but results in some oddities when I try to pass around arrays of any type. For example, String[] t = { "A", "B", "C", "D" }; reaches the web service as a an empty String (not String[]).

Does anyone have any idea what's going on here or how to get things working in any sensible way? I'm deploying locally to Apache Geronimo at the moment.

Server code snippet:

Interface:

@WebService
@SOAPBinding(style=Style.DOCUMENT, use=Use.LITERAL, parameterStyle=ParameterStyle.WRAPPED) 
@XmlSeeAlso({Feedback.class,Feedback[].class})
public interface CerberusRequest {

...

    @WebMethod
    public Feedback[] test(String[] facts) throws Exception;

}

Implementation:

@WebService(serviceName = "CerberusRequestService", portName
    = "CerberusRequestPort", targetNamespace = "http://web.cerberus.xyz.gov/",
    endpointInterface = "gov.xyz.cerberus.web.CerberusRequest")
public class CerberusRequestImpl implements CerberusRequest {
    ...
    public Feedback[] test(String[] facts) throws Exception {
        //Just some debug crap (prints: "in test...1 [Ljava.lang.String;")
        System.out.println("in test..." + facts.length + " " + facts.getClass().getName());
        for(String f : facts) {
            System.out.println("test:" + f);
        }
        //return feedback;
        Feedback[] f = { new Feedback() };
        return f;
    }
}

Client code snippet:

URL url = new URL(destination+"?wsdl");
QName qname = new QName(namespace, service);

Service service = Service.create(url, qname);
CerberusRequest sr = service.getPort(CerberusRequest.class);

String[] t = { "A", "B", "C", "D" };
Feedback[] feedback = sr.test(t);
for(Feedback f : feedback) {
    System.out.println(f);
}

WSDL:

<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://web.cerberus.xyz.gov/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CerberusRequestService" targetNamespace="http://web.cerberus.xyz.gov/">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://web.cerberus.xyz.gov/" schemaLocation="http://localhost:8080/CerberusService/CerberusRequest?xsd=xsd1"/>
    </xsd:schema>
    <xsd:schema>
      <xsd:import namespace="http://jaxb.dev.java.net/array" schemaLocation="http://localhost:8080/CerberusService/CerberusRequest?xsd=xsd2"/>
    </xsd:schema>
  </types>

  <message name="Exception">
    <part element="tns:Exception" name="fault">
    </part>
  </message>
  <message name="test">
    <part element="tns:test" name="parameters">
    </part>
  </message>
  <message name="testResponse">

    <part element="tns:testResponse" name="parameters">
    </part>
  </message>
  <portType name="CerberusRequest">
    <operation name="test">
      <input message="tns:test">
    </input>
      <output message="tns:testResponse">
    </output>

      <fault message="tns:Exception" name="Exception">
    </fault>
    </operation>
  </portType>
  <binding name="CerberusRequestPortBinding" type="tns:CerberusRequest">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="test">
      <soap:operation soapAction=""/>
      <input>

        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
      <fault name="Exception">
        <soap:fault name="Exception" use="literal"/>
      </fault>
    </operation>

  </binding>
  <service name="CerberusRequestService">
    <port binding="tns:CerberusRequestPortBinding" name="CerberusRequestPort">
      <soap:address location="http://localhost:8080/CerberusService/CerberusRequest"/>
    </port>
  </service>
</definitions>

Thank you very much in advance for any and all help you can provide!

解决方案

Well I had similar kind of issues I wanted to pass the VO , which had a complex object (map) inside and this map could contain map of maps as values or List of Map.

Even if you support the current requirement you may end up in other issues later. Better you can use third party serializers like Castor/Dozer/GSon kind of tools. And change the data a simple java type which can be easily passed over the wire.

这篇关于如何使用Java中的Web服务(例如Axis2)发送复杂对象的数组或集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆