學習WebService進行中......
今天總結一下前段時間學習WebServices的經驗和成果......hehe
.........首先進行服務器端的編碼
環境MyEclipse(服務器端)+Eclipse(客戶端)+tomcat+jdk1.6+xFire
1....................創建工程
MyEclipse中集成了Xfire框架,這樣給我們省了不少麻煩hh
這樣得到了兩個src中的文件一個接口和一個實現類
.......接口
package com.demo;
//Generated by MyEclipse
public interface IHelloService {
public String example(String username);
}
.......實現類
package com.demo;
//Generated by MyEclipse
public class HelloServiceImpl implements IHelloService {
public String example(String username) {
return username+"我來了!";
}
}
(做了點小動作....別見怪)
2................大頭戲來了,用戶驗證監聽處理器代碼(大多通用直接copy吧)
package com.client;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.Element;
public class ClientAuthenticationHandler extends AbstractHandler {
private String username = null;
private String password = null;
public ClientAuthenticationHandler() {
}
public ClientAuthenticationHandler(String username,String password) {
this.username = username;
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void invoke(MessageContext context) throws Exception {
//爲SOAP Header構造驗證信息
Element el = new Element("header");
context.getOutMessage().setHeader(el);
Element auth = new Element("AuthenticationToken");
Element username_el = new Element("Username");
username_el.addContent(username);
Element password_el = new Element("Password");
password_el.addContent(password);
auth.addContent(username_el);
auth.addContent(password_el);
el.addContent(auth);
}
}
3.....................服務端收尾,配置service.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>HelloService</name>
<serviceClass>com.demo.IHelloService</serviceClass>
<implementationClass>
com.demo.HelloServiceImpl
</implementationClass>
<!--註冊監聽處理器-->
<inHandlers>
<handler handlerClass="com.handler.AuthentificationHandler"></handler>
</inHandlers>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
</beans>
4......................服務端完成了,大家看看我的文件結構吧
5............................先給大家點驚喜,服務端配置好了,啓動一下tomcat把我們的webService服務發佈一下,
在瀏覽器中輸入URL http://localhost:8080/WebService/services/HelloService?wsdl 如果顯示
這個xml頁面的話,恭喜你,發佈成功了。
6.......................下面來看一下客戶端如何進行編寫
首先需要把xfire的架包導入工程(放到lib文件夾下)
先看看我的文件夾結構
在這裏我用了config.properties進行存放接口路徑(有點大材小用)
7...........................首先需要編寫的是接口文件IHelloService.java(可以直接拷貝服務器端的)
package com.imp;
public interface IHelloService {
public String example(String username);
}
8................................然後編寫PropertiesConfig用來讀取properties文件
package com.test;
import java.io.FileInputStream;
import java.util.Properties;
public class PropertiesConfig {
public static String site;
public static String getsite() {
Properties p = new Properties();
try {
p.load(new FileInputStream("src/config.properties"));
} catch (Exception e) {
e.printStackTrace();
}
site = p.getProperty("webservice_site");
return site;
}
}
9........................................下面是客戶端身份驗證代碼ClientAuthenticationHandler.java
package com.client;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.Element;
public class ClientAuthenticationHandler extends AbstractHandler {
private String username = null;
private String password = null;
public ClientAuthenticationHandler() {
}
public ClientAuthenticationHandler(String username,String password) {
this.username = username;
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void invoke(MessageContext context) throws Exception {
//爲SOAP Header構造驗證信息
Element el = new Element("header");
context.getOutMessage().setHeader(el);
Element auth = new Element("AuthenticationToken");
Element username_el = new Element("Username");
username_el.addContent(username);
Element password_el = new Element("Password");
password_el.addContent(password);
auth.addContent(username_el);
auth.addContent(password_el);
el.addContent(auth);
}
}
10....................................最後來到進行我們的接口調用吧
package com.test;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import org.codehaus.xfire.client.*;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.client.ClientAuthenticationHandler;
import com.imp.IHelloService;
public class ClientTest {
/**
* @param args
* @throws MalformedURLException
*/
public static void main(String[] args) {
try {
// 獲取properties中的配置url地址
String serviceUrl = PropertiesConfig.getsite();// 新增
Service serviceModel = new ObjectServiceFactory().create(
IHelloService.class, null, serviceUrl + "?wsdl", null);
IHelloService service = (IHelloService) new XFireProxyFactory()
.create(serviceModel, serviceUrl);
XFireProxy proxy = (XFireProxy) Proxy.getInvocationHandler(service);
Client client = proxy.getClient();
// 發送授權信息
client.addOutHandler(new ClientAuthenticationHandler("admin",
"admin"));
// 輸出調用web services方法的返回信息
System.out.println(service.example("admin"));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}
好了,代碼部分到此爲止
如果你的服務器還沒有停止,那麼我們現在可以來測試了
運行客戶端程序
11...........................................客戶端顯示
12.....................................服務器端顯示
13................................今天又進步一點.....................................good good study ,day day up .