一、基于EJB容器管理webservice:
1.首先建立一个Web services EndPoint:
packagecn.test.service.impl;importjava.util.ArrayList;importjava.util.List;importjavax.jws.WebMethod;importjavax.jws.WebService;importcn.test.Person;importcn.test.service.HelloWorld;
@WebServicepublic class HelloWorldImpl implementsHelloWorld {
@WebMethodpublic voidsay() {
System.out.println(" hello service");
}
@WebMethodpublicPerson getPersonById( String id) {
System.out.println(" id = "+id);return new Person("aaa", "123", 5);
}
@WebMethodpublic ListgetAll() {
List list = new ArrayList();for (int i = 0; i < 5; i++) {
list.add(new Person("100"+i, "kk"+i, 12+i));
}returnlist;
}
@WebMethodpublicString insert(Person p) {
System.out.println(p);return p.getId()+"java";
}
}
2. 进入src目录下使用apt编译Hello.java(例:apt -d [存放编译后的文件目录] Hello.java ) ,会生成 jaws目录。例如:D:\dianzi\Test\src>apt -d . cn/test/service/impl/HelloWorldImpl.java
3.使用java Hello.Hello运行,然后将浏览器指向http://localhost:8080/hello?wsdl就会出现下列显示
4.使用wsimport 生成客户端
使用如下:D:\dianzi\Test\src>wsimport -p cn.client -keep http://localhost:8080/helloWorld?wsdl
这时,会在当前目录中生成如下文件:
5.客户端测试:
public classT {public static voidmain(String[] args) {
HelloWorldImplService service= newHelloWorldImplService();
HelloWorldImpl helloWorld=(HelloWorldImpl) service.getHelloWorldImplPort();
System.out.println(helloWorld.getAll().size());
System.out.println(helloWorld.getPersonById(""));
}
}
6、不用生成代码,通过jboss服务器进行管理:
例如下面的类写好之后,发布到服务器上,通过地址 http://127.0.0.1/root/CompanyService?wsdl 就可以查看发布情况,其中“contextRoot”表示根目录,urlPattern表示匹配路径。
@Stateless
@WebService
@WebContext(contextRoot= "/root", urlPattern = "/CompanyService")
@SOAPBinding(style= SOAPBinding.Style.RPC,use=SOAPBinding.Use.LITERAL, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)public classCompanyWSBean implements CompanyWS {
Logger logger=Logger.getLogger(CompanyWSBean.class);
@EJBprivateCompanyServiceLocal companyServiceLocal;
@WebMethod
@WebResult(partName= "return")publicTBLCompany find(String id) {try{returncompanyServiceLocal.find(id);
}catch(Exception e) {
logger.error(e);
}return null;
}
}
二、将第一种webservice移动到 WEB层,就是通过WEB容器模式管理:
1、准备pojo对像:接口和实现类
开发步骤如下:
建立一个POJO端点--在web.xml中--------将这个POJO端点配置成一个Servlet-----------将这个POJO端点打包成一个应用(war包或ear)
packagecn.test.ws.web.intf;importcom.cjonline.foundation.evisa.entity.TBLOperator;public interfaceHelloWorld {publicString say(String s);publicString eat(String s);publicTBLOperator findOperator();
}
packagecn.test.ws.web.impl;importjavax.jws.WebMethod;importjavax.jws.WebService;importcn.test.ws.web.intf.HelloWorld;importcom.cjonline.comm.util.EJBAssistant;importcom.cjonline.foundation.evisa.entity.TBLOperator;importcom.cjonline.foundation.evisa.exception.ServiceException;importcom.cjonline.foundation.evisa.service.intf.OperatorServiceRemote;
@WebServicepublic class HelloWorldImpl implementsHelloWorld{private OperatorServiceRemote userService =EJBAssistant.getEJB("OperatorServiceBean/remote", OperatorServiceRemote.class);
@WebMethodpublicString say(String s) {
System.out.println(" web say : "+s);return " web say : "+s;
}
@WebMethodpublicString eat(String s) {
System.out.println(" web eat : "+s);return " web eat : "+s;
}
@WebMethodpublicTBLOperator findOperator(){try{return userService.findAll1().get(0);
}catch(ServiceException e) {
e.printStackTrace();
}return null;
}
}
2、在web.xml中将它配置成一个Servlet:
HelloService
cn.test.ws.web.impl.HelloWorldImpl
HelloService
/hello
3、进入到jbossws管理服务中,通过地址 http://lenovo-pc//hello?wsdl可以查看发布的信息,就可以调用方法。
三、使用xfire,我这里使用的是myeclipse集成的xfire进行测试的:
利用xfire开发WebService,可以有三种方法:
1一种是从javabean 中生成;
2 一种是从wsdl文件中生成;
3 还有一种是自己建立webservice
步骤如下:
用myeclipse建立webservice工程,目录结构如下:
首先建立webservice接口,代码如下:
1
packagecom.myeclipse.wsExample;2
//Generated by MyEclipse3
4
publicinterfaceIHelloWorldService{5
6
publicString example(String message);7
8
}
接着实现这个借口:
1
packagecom.myeclipse.wsExample;2
//Generated by MyEclipse3
4
publicclassHelloWorldServiceImplimplementsIHelloWorldService{5
6
publicString example(String message){7
returnmessage;8
}9
10
}
修改service.xml 文件,加入以下代码:
1
2
HelloWorldService3