一、JDK1.6自带的JWS发布WebService及测试
1.定义一个接口
package com.test.service;
import javax.jws.WebService;
@WebService
public interface IWebService {
int add(int a, int b);
int subtract(int a, int b);
}
2.编写实现类
package com.test.service.impl;
import javax.jws.WebService;
import com.test.service.IWebService;
@WebService(endpointInterface="com.test.service.IWebService")
public class WebServiceImpl implements IWebService{
@Override
public int add(int a, int b) {
System.out.println(a + "+" + b + "=" + (a + b));
return a + b;
}
@Override
public int subtract(int a, int b) {
System.out.println(a + "-" + b + "=" + (a - b));
return a - b;
}
}
3.发布WebService
package com.test.test;
import javax.xml.ws.Endpoint;
import com.test.service.impl.WebServiceImpl;
public class Test {
public static void main(String[] args) {
String address = "http://localhost:8888/test";
Endpoint.publish(address, new WebServiceImpl());
}
}
4.编写客户端进行测试
package com.test.test;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.test.service.IWebService;
public class TestClient {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8888/test?wsdl");
QName name = new QName("http://impl.service.test.com/","WebServiceImplService");
Service service = Service.create(url,name);
IWebService endPoint = service.getPort(IWebService.class);
endPoint.add(10, -14);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
经测试、一切OK。至此,JWS发布WebService的编写、发布和测试结束。
备注:JWS是Java语言对WebService服务的一种实现,用来开发和发布服务。而从服务本身的角度来看JWS服务是没有语言界限的。但是Java语言为Java开发者提供便捷发布和调用WebService服务的一种途径。