在不调用Web服务的情况下获取soap消息

时间:2011-04-17 20:11:55

标签: java soap jax-ws java-metro-framework

使用Glassfish Metro实现JAX-WS规范,是否可以为特定操作生成SOAP请求消息,而无需实际调用该操作。像SOAPUI这样的东西只能生成基于WSDL的样本SOAP消息,我想生成它,提供操作参数。

感谢。

2 个答案:

答案 0 :(得分:1)

行。我想我已经明白了。它不漂亮而且它不干净,因为它使用反射,基于Oracle专有类并假设您已经生成了客户端WS部分,但是如果您需要这样的功能,就像我一样严重,截止日期即将到来不可避免死亡本身然后听我的故事:)

// 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, of, your, 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();
}

所以再一次这是一个非常糟糕的解决方案,但是直到一些沉重的思想家来,并且用更好的东西来保存我的一天,或者Sun移动课程,我需要更友好的包装才能满足。

答案 1 :(得分:0)

DIY:将客户端指向转储有效负载的PHP页面。运行客户端。它将无法读取响应,但请求将被保存。