lzp2014
新手村菜鸟
2017-05-11 09:43

如何使用jax-ws APIs 实现webservice client

  • java
  • java-ee
  • web service

String wsdlUrl="http://localhost:8080/springmvc/services/helloWebService?wsdl";
QName serviceName=new QName("http://springmvc.com/","helloWebService");
Service service=Service.create(serviceName);
service.addPort(serviceName, javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING,wsdlUrl);
Dispatch dispatch=service.createDispatch(serviceName,SOAPMessage.class,Service.Mode.MESSAGE);
MessageFactory messageFactory=MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);

    SOAPMessage request=messageFactory.createMessage();
    SOAPPart part=request.getSOAPPart();

    SOAPEnvelope soapEnvelope=part.getEnvelope();
    SOAPHeader header=soapEnvelope.getHeader();
    SOAPBody body=soapEnvelope.getBody();

    SOAPElement operation=body.addChildElement("hello","nsl","http://springmvc.com/");
    SOAPElement value=operation.addChildElement("name");
    value.addTextNode("a");
    request.saveChanges();

    SOAPMessage response=dispatch.invoke(request);

    System.out.println(response.getSOAPBody());


            这是参考官方文档写的,但并不能成功调用webservice 接口。希望是使用jax-ws APIs代码的方式调用,而不是wsimport自动生成代码

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答