cxf入门

一、CXF官方网址:http://cxf.apache.org/,到这去下载最新的版本,解压
二、创建一个工程,从解压的lib文件夹下导入如下包:
cxf-2.5.1.jar
wsdl4j-1.6.2.jar
xmlschema-core-2.0.1.jar
jetty-server-7.5.3.v20111011.jar
jetty-util-7.5.3.v20111011.jar
jetty-continuation-7.5.3.v20111011.jar
jetty-http-7.5.3.v20111011.jar
jetty-io-7.5.3.v20111011.jar
neethi-3.0.1.jar
三、创建服务器端代码
1、接口Login.java

package cxf.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface Login {
public String login(@WebParam(name="name")String name,@WebParam(name="password")String password);
}

2、实现类LoginImpl.java

package cxf.service;

import javax.jws.WebService;


@WebService(endpointInterface="cxf.service.Login",serviceName="Login")
public class LoginImpl implements Login {

public String login(String name, String password) {
String result = "登录CXF 服务端成功!";
if (!"cxf".equalsIgnoreCase(name) || !"cxf".equalsIgnoreCase(password)) {
result = "用户名或密码不正确,请重新登录!";
}
return result;
}

}

3、发布

package cxf.service;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class Pub {
public static void main(String[] args) {
LoginImpl loginImpl = new LoginImpl();
JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
bean.setServiceClass(Login.class);
bean.setServiceBean(loginImpl);
bean.setAddress("http://localhost:9000/helloWorld");
bean.create();
System.out.println("Finish....");
}
}

将产生如下信息:

2012-1-19 12:28:22 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf/}LoginService from class cxf.service.Login
2012-1-19 12:28:22 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:9000/helloWorld
2012-01-19 12:28:22.591:INFO:oejs.Server:jetty-7.5.3.v20111011
2012-01-19 12:28:22.669:INFO:oejs.AbstractConnector:Started SelectChannelConnector@localhost:9000 STARTING
2012-01-19 12:28:22.685:INFO:oejsh.ContextHandler:started o.e.j.s.h.ContextHandler{,null}
Finish....


四、创建客户端代码
1、定制客户端调用WebService的接口,这个接口中的方法签名和参数信息可以从wsdl中的内容看到:Login.java

package cxf.client;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace = "http://service.cxf/")
public interface Login {
public String login(@WebParam(name="name")String name,@WebParam(name="password")String password);
}

2、编写测试类:

package cxf.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();
bean.setServiceClass(Login.class);
bean.setAddress("http://localhost:9000/helloWorld");
Login login = (Login) bean.create();
System.out.println("result--->"+login.login("cxf", "cxf"));
}
}

将产生如下信息:

2012-1-19 12:28:55 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf/}LoginService from class cxf.client.Login
result--->登录CXF 服务端成功!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值