package com.lul.service;
import javax.jws.WebService;
@WebService
public interface IMyService {
int add(int a, int b);
int minus(int a, int b);
}
package com.lul.service;
import javax.jws.WebService;
@WebService(endpointInterface="com.lul.service.IMyService")
public class MyServiceImpl implements IMyService {
public int add(int a, int b) {
System.out.println(a + "+" + b + "=" + (a+b));
return a+b;
}
public int minus(int a, int b) {
System.out.println(a + "-" + b + "=" + (a-b));
return a-b;
}
}
package com.lul.service;
import javax.xml.ws.Endpoint;
public class MyService {
public static void main(String[] args) {
String address = "https://localhost:8888/ns";
Endpoint.publish(address, new MyServiceImpl());
System.out.println("Service published at " + address);
}
}
運行後訪問 https://localhost:8888/ns?wsdl 可見 WSDL 文檔。
package com.lul.service;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class TestClient {
public static void main(String[] args) throws Exception {
URL url = new URL("https://localhost:8888/ns?wsdl");
QName qname = new QName("https://service.lul.com/", "MyServiceImplService");
Service service = Service.create(url, qname);
IMyService proxy = service.getPort(IMyService.class);
System.out.println("10+9=" + proxy.add(10, 9));
}
}
@WebService 註解標識服務類/接口。Endpoint.publish 一鍵發布,內置輕量級 HTTP 服務器(適合測試,生產用 Tomcat/CXF/Axis2)。targetNamespace 一致。