您的位置:首页 > 编程语言 > Java开发

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

2009-10-21 16:58 681 查看
使用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)在最后個標簽后面添加

wsdl
text/xml

xsd
text/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文件,文件內容如下:

"http://www.springframework.org/dtd/spring-beans.dtd">

org.apache.axis.client.ServiceFactory


"http://www.springframework.org/dtd/spring-beans.dtd">

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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: