import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHello(@WebParam(name="username")String username);
}
import javax.jws.WebService;
@WebService(serviceName="HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String username) {
return "Hello:"+username;
}
}
import javax.xml.ws.Endpoint;
public class Server {
/**
* @param args
*/
public static void main(String[] args) {
HelloWorld hello=new HelloWorldImpl();
String address="http://localhost:8081/ws/HelloWorldService";
Endpoint.publish(address, hello);
System.out.println("Servet start...");
}
}
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
public class Client {
private static final QName SERVICE_NAME=new QName(
"http://endpoint.cxf.webservice.test/",//xmlns:tns="http://endpoint.cxf.webservice.test/"
"HelloWorld"//<wsdl:binding name="helloWorldServiceSoapBinding" type="tns:HelloWorldServiceInf">
);
private static final QName PORT_NAME=new QName(
"http://endpoint.cxf.webservice.test/",//xmlns:tns="http://endpoint.cxf.webservice.test/"
"HelloWorldImplPort"//<wsdl:port binding="tns:helloWorldServiceSoapBinding" name="HelloWorldServiceImplPort">
);
public static void main(String[] args) {
String endPointAddress="http://localhost:8081/ws/HelloWorldService";
Service service=Service.create(SERVICE_NAME);
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endPointAddress);
HelloWorld inf=service.getPort(HelloWorld.class);
System.out.println(inf.sayHello("张三"));
}
}
----------------------------------------------------------------
执行Client报异常:java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.
解决方法:
HelloWorld inf=service.getPort(HelloWorld.class);--------------->HelloWorld inf=service.getPort(PORT_NAME, HelloWorld.class);