Java 在不调用web服务的情况下获取soap消息
使用JAX-WS规范的Glassfish Metro实现,可以为特定操作生成SOAP请求消息,而无需实际调用该操作。类似于SOAPUI的功能,仅基于WSDL生成示例SOAP消息,我希望生成它,为操作提供参数Java 在不调用web服务的情况下获取soap消息,java,soap,jax-ws,java-metro-framework,Java,Soap,Jax Ws,Java Metro Framework,使用JAX-WS规范的Glassfish Metro实现,可以为特定操作生成SOAP请求消息,而无需实际调用该操作。类似于SOAPUI的功能,仅基于WSDL生成示例SOAP消息,我希望生成它,为操作提供参数 谢谢。DIY:将客户端指向转储负载的PHP页面。运行客户端。读取响应将失败,但请求将被保存。确定。我想我明白了。它不漂亮,也不干净,因为它使用反射,基于Oracle专有类,并假设您已经生成了客户端WS-part,但如果您像我一样迫切需要此类功能,因为最后期限即将到来,就像死亡本身一样,那么请
谢谢。DIY:将客户端指向转储负载的PHP页面。运行客户端。读取响应将失败,但请求将被保存。确定。我想我明白了。它不漂亮,也不干净,因为它使用反射,基于Oracle专有类,并假设您已经生成了客户端WS-part,但如果您像我一样迫切需要此类功能,因为最后期限即将到来,就像死亡本身一样,那么请听我的故事:)
//以URL格式提供的wsdl文件的位置
//前。file://localhost/C:/wsdl.wsdl 用于本地文件
字符串wsdlLocation=“wsdlLocation”;
试一试{
//我们假设您已经生成了WS客户端
GeneratedService=新生成的服务(
新URL(wsdlLocation),
新的QName(“namespaceURI”、“localPart”);
GeneratedPort=service.getGeneratedPort();
SEIStub=(SEIStub)Proxy.getInvocationHandler(端口);
字段方法Handlersfield=
stub.getClass().getDeclaredField(“methodHandlers”);
//黑客使私人领域的访问
methodHandlersField.setAccessible(true);
方法operationMethod=null;
对象args=null;
切换(向您发送消息的方式){
案例值:
operationMethod=GeneratedPort.class.getMethod(
“methodName”,类,of,您的属性);
args=新对象[]{attributes,of,your,method};
打破
违约:
抛出新的SomeException(“somemessage”);
打破
}
MethodHandler=((映射)methodHandlersField。
get(存根)).get(操作方法);
方法createMessageMethod=handler.getClass().getSuperclass()。
getDeclaredMethod(“createRequestMessage”,对象[].class);
//另一个黑客
createMessageMethod.setAccessible(true);
Message Message=(Message)createMessageMethod.invoke(handler,args);
变压器=
TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,“是”);
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}缩进金额,“2”);
变形金刚(
message.readPayloadAsSource(),新的StreamResult(System.out));
}捕获(例外e){
//有很多东西要抓
e、 printStackTrace();
}
因此,这再一次是一个非常糟糕的解决方案,但在一些思想沉重的人来用更好的东西或Sun移动类拯救我的一天之前,我需要更友好的软件包,这就足够了。如果我想让我的应用程序依赖外部服务,那么我只会坚持我的web服务,由于Jax-WS允许我将消息处理程序添加到处理程序链中,所以从处理程序链内部抛出RuntimeException,停止进一步的操作调用,并在任何我想要的地方捕获该异常,其中消息是异常的一个字段。然而,在我的处理程序有机会做任何事情之前,这个解决方案要求在方法调用程序连接到WS时存在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();
}