创建第一个WebService程序
1.首先创建实现类IMyService
package org.aa;
import javax.jws.WebService;
/*SEI(Service EndPoint Interface)
*SIB(Service Implemention Bean)
*在这个例子中IMyService就是SEI,即服务实现的接口,MyServiceImpl就是SIB,即服务实现的Bean
* */
@WebService()
public interface IMyService {
//相加接口
public int add(int a ,int b);
//相减接口
public int minus(int a , int b);
}
2.创建IMyService接口的实现类MyServiceImpl
package org.aa;
import javax.jws.WebService;
//配置好我的接口
@WebService(endpointInterface="org.aa.IMyService")
//实现IMyService接口
public class MyServiceImpl implements IMyService {
@Override
public int add(int a, int b) {
System.out.println(a + "+" + b + "=" + (a+b));
return a+b;
}
@Override
public int minus(int a, int b) {
System.out.println(a + "-" + b + "=" + (a-b));
return a-b;
}
}
3.创建WebService服务MyService
package org.aa;
import javax.xml.ws.Endpoint;
public class MyServer {
public static void main(String[] args){
//首先确定一个在网上发布的地址
String address = "http://localhost:8888/ns";
//发布一下,两个参数,前一个是在网上发布的地址,后一个为要发布接口的实现类
Endpoint.publish(address,new MyServiceImpl());
}
}
4.出现了这个错误
Exception in thread "main" com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class org.aa.jaxws.Add is not found. Have you run APT to generate them?
at com.sun.xml.internal.ws.model.RuntimeModeler.getClass(RuntimeModeler.java:256)
at com.sun.xml.internal.ws.model.RuntimeModeler.processDocWrappedMethod(RuntimeModeler.java:567)
at com.sun.xml.internal.ws.model.RuntimeModeler.processMethod(RuntimeModeler.java:514)
at com.sun.xml.internal.ws.model.RuntimeModeler.processClass(RuntimeModeler.java:341)
at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:227)
at com.sun.xml.internal.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:308)
at com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:174)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(WSEndpoint.java:420)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(WSEndpoint.java:439)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(EndpointImpl.java:208)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:138)
at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:92)
at javax.xml.ws.Endpoint.publish(Endpoint.java:170)
at org.aa.MyServer.main(MyServer.java:10)
原因:JDK1.6早起版本对这个类型的支持有bug,所以报错。
解决办法:只要将JDK升级到1.6.0_22(包括这个版本)以后,再设置一下MyEclipse默认的jdk就解决问题了,其他的办法不 好解决。
5.创建一个类TestClient调用WebService提供的服务(必须注意测试运行TestClient时必须把MyService服务运行起来)
package org.aa;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class TestClient {
public static void main(String[] args){
try {
URL url = new URL("http://localhost:8888/ns?wsdl");
//new QName两个参数,第一个是目标命名空间即targetNamespace="http://aa.org/"中的http://aa.org/
//另一个是本地WebService提供的名称即name="MyServiceImplService"中的MyServiceImplService
QName sname = new QName("http://aa.org/","MyServiceImplService");
//创建Service服务
Service service = Service.create(url,sname);
//得到IMyService接口
IMyService ms = service.getPort(IMyService.class);
System.out.println(ms.add(1, 2));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
创建第一个WebService程序
最新推荐文章于 2022-10-20 18:10:53 发布