使用Axis1.4 和 Spring2.5.6搭建最简易的Webservice及如何在Windchill 7.0中配置(一)

使用Axis1.4 和 Spring2.5.6搭建最简易的Webservice及如何在Windchill 7.0中配置 其中Tomcat版本为:4.1版 1.配置开发及测试环境 1)在Eclipse中建立Web工程(为方便介绍这里取名为Demo),并将如下包添加至Build Path中 axis包中: axis.jar; axis-ant.jar; commons-discovery-0.2.jar; commons-logging-1.0.4.jar; jaxrpc.jar; log4j-1.2.8.jar; saaj.jar; wsdl4j.jar spring-2.5.6包中:spring.jar; spring-agent.jar; spring-aspects.jar; spring-tomcat-weaver.jar spring-ws-1.5.8包中:spring-ws-1.5.8-all.jar 2)将如上axis包中全部jar和spring.jar当靠贝加入%Windchill_Home%/codebase/WEB-INF/lib下 2.在Demo工程中分别建如下类 ###DemoEntity.java### package entity; import java.io.Serializable; import java.util.Date; public class DemoEntity implements Serializable{ //需实现序列化 private String name; private Date createDate; public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public String getName() { return name; } public void setName(String name) { this.name = name; } } ###DemoService.java### package service; import entity.DemoEntity; /** * 给外部提供服务的接口 * @author jason 2009/10/20 */ public interface DemoService { public void hello(String word); public String showEntity(DemoEntity[] entitys); } ###DemoServiceImpl.java### package service.impl; import service.DemoService; import entity.DemoEntity; public class DemoServiceImpl implements DemoService{ public void hello(String word){ System.out.println("DemoService say: " + word); } public String showEntity(DemoEntity[] entitys){ for(int i = 0; i < entitys.length; i++) if(entitys[i] != null) System.out.println("DemoService get Entity Name:" + entitys[i].getName() + "; Date:" + entitys[i].getCreateDate()); else return "entitys " + i + " is Null"; return "OK"; } } ###DemoServiceEndPoint.java### package webservice; import org.springframework.remoting.jaxrpc.ServletEndpointSupport; import service.DemoService; import entity.DemoEntity; public class DemoServiceEndPoint extends ServletEndpointSupport implements DemoService{ private DemoService demoService; public DemoServiceEndPoint() { //测试 System.out.println("DemoServiceEndPoint is initialized..."); } protected void onInit() { System.out.println("Inside FmeaSvcEndPoint.onInit..."); //此处getBean方法中参数"demoService"与步骤4创建的文件中bean id名称对应 this.demoService = (DemoService) getWebApplicationContext().getBean( "demoService"); } public void hello(String word){ demoService.hello(word); } public String showEntity(DemoEntity[] entitys){ return demoService.showEntity(entitys); } } 3.在Eclipse中对DemoServiceEndPoint建立webservice 1)右键Demo工程src下DemoServiceEndPoint.java文件 2)[Web Services]->[Create Web Service] 3)[Web service type]选为:Bottom up Java bean Web service 4)需注意[Service implementation]填写继承ServletEndpointSupport的类, 如此Demo填写应为:webservice.DemoServiceEndPoint 5)Configuration为: Server: Tomcat v4.1 Server Web service runtime: Apache axis Service project: Demo 6)左键单击[Next] 7)此时会弹出一个警告窗口,左键单击[Details]显示如下信息: The service class "webservice.DemoServiceEndPoint" does not comply to one or more requirements of the JAX-RPC 1.1 specification, and may not deploy or function correctly. The method "init" on the service class "webservice.DemoServiceEndPoint" uses a data type, "java.lang.Object", that is not supported by the JAX-RPC specification. Instances of the type may not serialize or deserialize correctly. Loss of data or complete failure of the Web service may result. 8)左键点击[Ok] 9)在[Methods]多选框中,取消选中多选框中的init(java.lang.Object)和destroy()选项 其它默认即可 10)左键点击[Next]->[Finish] 4.在Demo工程/WebContent/WEB-INF/路径下建立applicationContext.xml文件,内容如下: 5.修改Demo工程下/WebContent/WEB-INF/web.xml文件 1)将再添加至首个标签的前面如下代码 contextConfigLocation /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener 2)在最后个标签后面添加 wsdltext/xmlxsdtext/xml 说明:如果不设定,则会报如下错误 AxisFault faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException faultSubcode: faultString: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? faultActor: faultNode: 6.查看创建Webservice后自动创建文件完成情况(路径根据开发IDE可能有所不同),我的如下: 1)/WebContent/wsdl/DemoServiceEndPoint.wsdl 2)/WebContent/WEB-INF/DemoServiceEndPointService/webservice路径下有三个文件: deploy.wsdd; deploy.wsdd.bak; undeploy.wsdd 3)/WebContent/WEB-INF/server-config.wsdd 本应该有此文件的,可现在不知道为何我的Eclipse没有创建此文件 如果没有创建此文件就至补充 7.将如上java文件编译后拷贝至%Windchill%/codebase路径下, 将步骤6中2)和3)文件拷贝至%Windchill%/codebase/WEB-INF中, 将web.xml文件中添加内容拷贝至%Windchill%/codebase/WEB-INF/web.xml中 8.在%Apache_Home%/conf/app-Windchill.conf文件中的标签下加入如下 JkMount /Windchill/services/* ajp13 此处是设定Http访问路径符合/Windchill/services/*模式的将由ajp13转发至Tomcat处理响应 9.生成测试客户端 1)新建DemoClient项目 2)在Demo项目中的/WebContent/wsdl/DemoServiceEndPoint.wsdl文件右键 3)[Web Services]->[Generate Client] 4)左键点击[Configuration]下的[Client Project:Demo]链接 5)在弹出窗口中的[Client Project]下拉选项,选择DemoClient,左键点击[OK] 6)[Next]->[Finish] 7)在DemoClient项目中src下建立applicationContextClient.xml文件,文件内容如下: <?xml version="1.0" encoding="UTF-8"?> org.apache.axis.client.ServiceFactory <?xml version="1.0" encoding="UTF-8"?> org.apache.axis.client.ServiceFactory 8)建立测试java类如下: ###ClientDemo.java### package client; import java.rmi.RemoteException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import webservice.DemoServiceEndPoint; import entity.DemoEntity; import java.util.Calendar; public class ClientDemo { private ApplicationContext ctx; private DemoServiceEndPoint service; public ClientDemo(){ ctx = new ClassPathXmlApplicationContext("/applicationContextClient.xml"); service = (DemoServiceEndPoint)ctx.getBean("demoService"); } public void hello(String param) throws RemoteException { service.hello(param); } public String showEntity(DemoEntity[] entitys) throws RemoteException { return service.showEntity(entitys); } public static void main(String[] args) { ClientDemo demo = new ClientDemo(); try { DemoEntity[] entitys = new DemoEntity[2]; DemoEntity entity = new DemoEntity(); entity.setName("DemoOne"); entity.setCreateDate(Calendar.getInstance()); entitys[0] = entity; entity = new DemoEntity(); entity.setName("DemoTwo"); entity.setCreateDate(Calendar.getInstance()); entitys[1] = entity; demo.showEntity(entitys); demo.hello("Hello Demo"); } catch (RemoteException e) { e.printStackTrace(); } } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章