学习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 .