Java – get SOAP messages without invoking Web Services

Using the GlassFish Metro implementation of jax-ws specification, soap request messages can be generated for specific operations without actually invoking operations Things like soapUI can only generate sample SOAP messages based on WSDL. I want to generate it and provide operation parameters

thank you.

Solution

OK I think I already understand It's not beautiful and it's not clean because it uses reflection, based on Oracle proprietary classes and assuming that you have generated the client WS part, but if you need such a function, as serious as me, the deadline is coming, it's inevitable to die itself, and then listen to me:)

// location of wsdl file provided in URL format
// ex. file://localhost/C:/wsdl.wsdl for local file
String wsdlLocation = "wsdlLocation";

try{
    // we're assuming that you've already generated WS client side
    GeneratedService service = new GeneratedService(
        new URL(wsdlLocation),new QName("namespaceURI","localPart"));

    GeneratedPort port = service.getGeneratedPort();

    SEIStub stub = (SEIStub) Proxy.getInvocationHandler(port);

    Field methodHandlersField =
        stub.getClass().getDeclaredField("methodHandlers");
    //hack to make private field accessible
    methodHandlersField.setAccessible(true);

    Method operationMethod = null;
    Object args = null;

    switch (somethingToTellYouWhatMethodToInvoke){
        case someMethodValue:
            operationMethod = GeneratedPort.class.getmethod(
                "methodName",classes,of,your,attributes);
            args = new Object[]{attributes,method};
            break;
        default:
            throw new SomeException("some message");
            break;
    }

    MethodHandler handler = ((Map<Method,MethodHandler>) methodHandlersField.
        get(stub)).get(operationMethod);

    Method createMessageMethod = handler.getClass().getSuperclass().
        getDeclaredMethod("createRequestMessage",Object[].class);
    //another hack
    createMessageMethod.setAccessible(true);

    Message message = (Message) createMessageMethod.invoke(handler,args);

    Transformer transformer =
        TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT,"yes");
    transformer.setOutputProperty(
        "{http://xml.apache.org/xslt}indent-amount","2");
    transformer.transform(
        message.readPayloadAsSource(),new StreamResult(System.out));
} catch (Exception e){
    //lots of things to catch
    e.printStackTrace();
}

So again, this is a very bad solution, but until some heavy thinkers come and provide better things for my days or sun mobile courses, I need more friendly packaging to meet

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>