简体   繁体   English

从另一个调用Web服务

[英]Calling Web Service from another one

I have got SOAP service implementation with this action in it: 我已经有了执行此操作的SOAP服务:

public class SimpleWSAction extends AbstractActionPipelineProcessor{

public static final Logger logger = Logger.getLogger(SimpleWSAction.class);

private SimpleWSServiceImpl validator = new SimpleWSServiceImpl();
private SimpleWSDAOImpl clientVerificationService = new SimpleWSDAOImpl();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");

public SimpleWSAction(ConfigTree _configTree) {
    System.out.println(_configTree.toString());
}

@Process
public Message process(Message message) {   
    if(logger.isInfoEnabled()){ logger.info("SimpleWSAction - START TIME: "+ sdf.format(new Date(System.currentTimeMillis())));}
    SimpleWSRequest request = (SimpleWSRequest) message.getBody().get();
    ErrorContainer errorContainer = validator.validateRequest(request);

    if(!errorContainer.checkIfCriticalErrorOccured()){
        clientVerificationService.setPolicyToVerification(prepareDataToVerification(request), errorContainer);
    }
    SimpleWSResponse response = new SimpleWSResponse();

    TechnicalInputEnvelope technicalInputEnvelope = request.getTechnicalInputEnvelope();
    response.setTechnicalOutputEnvelope(getTechnicalOutputEnvelope(technicalInputEnvelope, errorContainer));
    message.getBody().add(response);
    if(logger.isInfoEnabled()){ logger.info("SimpleWSAction - END TIME: "+ sdf.format(new Date(System.currentTimeMillis())));}
    return message;
}

}

This is the most important part of it i think. 我认为这是最重要的部分。

here is my jboss-esb.xml im pasting it becouse im wondering if i need it for call 2nd service 这是我的jboss-esb.xml我正在粘贴它,因为我想知道我是否需要它来调用第二服务

<?xml version="1.0"?>
<jbossesb parameterReloadSecs="5"
xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.3.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.3.0.xsd http://anonsvn.jboss.org/repos/labs/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.3.0.xsd">
<services>
    <service category="SimpleWS"
        description="SimpleWS" invmScope="GLOBAL" name="SimpleWS">
        <security moduleName="SimpleWS"/>
        <property name="maxThreads" value="20" />
        <actions faultXsd="/contract/fault.xsd" inXsd="/contract/request.xsd" 
            mep="RequestResponse" outXsd="/contract/response.xsd">

            <action class="com.ist.esb.error.ErrorServiceAction" name="CAErrorService" process="process" />

            <action class="pl.warta.simplews.actions.JAXBAction" name="XmlToModel" process="fromXml">
                <property name="CLASS" value="pl.warta.simplews.model.request.SimpleWSRequest"/>
            </action>

            <action class="pl.warta.simplews.actions.SimpleWSAction" name="SimpleWSAction" />

            <action class="pl.warta.simplews.actions.JAXBAction"    name="ModelToXml" process="toXml">
                <property name="CLASS" value="pl.warta.simplews.model.response.SimpleWSResponse"/>
            </action>
    </service>
</services>

What is more i have got 2nd, similar service. 更重要的是,我得到了第二个类似的服务。 What i want to do is to call the 2nd service called AdvacedService (let us assume that it is doing the same job - it is copy of this with changed name of objects etc). 我想做的是调用名为AdvacedService的第二个服务(让我们假设它正在执行相同的工作-它是具有更改的对象名称等的副本)。 How to call it from this service? 如何通过此服务调用它? how about passing request from 1st service as a request for 2nd service or passing some variables? 将来自第一服务的请求作为第二服务的请求传递或传递一些变量怎么样? (2nd service is an other project btw) (第二项服务是另一个项目)

PS: im sorry for some non-english called variables PS:对某些非英语变量表示抱歉

i was thinking about calling it like this: 我正在考虑这样称呼它:

String wsdlURL = http://localhost:8080/AdvanceWSService/services/AdvanceWS?wsdl";
String namespace = "http://advancews.com";
String serviceName = "AdvanceWSService";
QName serviceQN = new QName(namespace, serviceName);

ServiceFactory serviceFactory = ServiceFactory.newInstance(); 
/* The "new URL(wsdlURL)" parameter is optional */
Service service = serviceFactory.createService(new URL(wsdlURL), serviceQN);

but how to pass a request or any parameter in this method? 但是如何在此方法中传递请求或任何参数?

You can't use the JaxWsDynamicClientFactory if you don't intend to provide it with complex types 如果您不打算为其提供复杂类型,则不能使用JaxWsDynamicClientFactory

Also, you technically don't have to import the Person type into the client. 另外,从技术上讲,您不必将“个人”类型导入客户端。 All you really need do is be aware of the type and use reflection to generate an instance of the class at runtime. 您真正需要做的就是了解类型,并在运行时使用反射生成类的实例。

The version of createClient you've used here is only good for webservice operations that accept simple types. 您在此处使用的createClient版本仅适用于接受简单类型的Web服务操作。 To be able to pass a complex type to a dynamic webservice client, 为了能够将复杂类型传递给动态Web服务客户端,

JaxWsDynamicClientFactory needs to dynamically generate the necessary support classes with the following: JaxWsDynamicClientFactory需要使用以下内容动态生成必要的支持类:

ClassLoader loader = this.getClass().getClassLoader();
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("wsdlPath", classLoader);

This creates the Client object and also the necessary pojos. 这将创建Client对象以及必要的pojos。

Then you'll be able to call the service with: 然后,您可以通过以下方式致电服务:

//Dynamically load an instance of the Model class. //动态加载Model类的实例。 You're not importing and you can simply configure the class name as an application property 您无需导入,只需将类名称配置为应用程序属性

Object model = Thread.currentThread().getContextClassLoader().loadClass("foo.bar.Model").newInstance(); 
Method theMethod = person.getClass().getMethod("setAge", Integer.class);
theMethod.invoke(person, 55); //set a property

client.invoke("testPerson", model); //invoke the operation.

You can try this out for passing parameter : 您可以尝试通过传递参数:

  import javax.xml.rpc.Call;
    import javax.xml.rpc.Service;
    import javax.xml.rpc.JAXRPCException;
    import javax.xml.namespace.QName;
    import javax.xml.rpc.ServiceFactory;
    import javax.xml.rpc.ParameterMode;

    public class DIIHello {

        private static String qnameService = "MyHelloService";
        private static String qnamePort = "MyHelloServiceRPC";
        private static String endpoint =
            "http://localhost:80/MyHelloService/MyHelloService";

        private static String BODY_NAMESPACE_VALUE =
            "urn:MyHelloService/wsdl";
        private static String ENCODING_STYLE_PROPERTY =
            "javax.xml.rpc.encodingstyle.namespace.uri";
        private static String NS_XSD =
            "http://www.w3.org/2001/XMLSchema";
        private static String URI_ENCODING =
            "http://schemas.xmlsoap.org/soap/encoding/";

        public static void main(String[] args) {
            try {

                ServiceFactory factory =
                    ServiceFactory.newInstance();
                Service service =
                    factory.createService(new QName(qnameService));

                QName port = new QName(qnamePort);

                Call call = service.createCall(port);
                call.setTargetEndpointAddress(endpoint);

                call.setProperty(Call.SOAPACTION_USE_PROPERTY,
                    new Boolean(true));
                call.setProperty(Call.SOAPACTION_URI_PROPERTY, "");
                call.setProperty(ENCODING_STYLE_PROPERTY,
                    URI_ENCODING);
                QName QNAME_TYPE_STRING = new QName(NS_XSD, "string");
                call.setReturnType(QNAME_TYPE_STRING);


                call.setOperationName(new QName(BODY_NAMESPACE_VALUE,
                    "sayHello"));
                 //For passing parameter
                call.addParameter("String_1", QNAME_TYPE_STRING,
                    ParameterMode.IN);
                String[] params = { "Kuntal" };

                String result = (String)call.invoke(params);
                System.out.println(result);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM