版本:CXF 2.7.0
jdk版本:1.6.0_38
IDE:Eclipse Java EE IDE Juno
因为是新手 所以这个主要是一个记录的日志 原理我还不太懂了... ...%>_<%
初期准备:
变量配置:
classpath cxf的lib文件夹
CXF_HOME 指向CXF根目录的路径
PATH 放入CXF的bin路径
java_home 环境变量(一定要是jre的 不是jdk的)
安装:
eclipse-->window-->preference-->web service-->CXF 2.x Preferences-->add 将CXF的根目录输入
服务器配置:
因为是第一个 就按照教程做一个最简单的 折扣服务
远端传入编号和原价 返回折扣价
步骤:
新建一个项目server
建立一个包
新建一个接口类作为服务的接口
新建一个类实现这个接口
代码如下:
接口类:
package org.cc.ws;
import javax.jws.WebService;
@WebService
public interface DiscountService {
// 折扣
public double discountPrice(int num,double original);
}
实现类:
package org.cc.ws;
public class DiscountServiceImpl implements DiscountService {
// 三级的折扣
private final int[] nums={
1,2,3
};
@Override
public double discountPrice(int num, double original) {
double price=original;
for(int numId:nums){
if(num==numId){
price*=(10-num)/10.0;
break;
}
}
return price;
}
}
启动类:
package org.cc.ws;
import javax.xml.ws.Endpoint;
public class Server {
public static void main(String[] args) {
// 简单的发布
// 参数一是发布的地址,参数二是服务接口实现类的对象
Endpoint.publish("http://localhost/discount", new DiscountServiceImpl());
}
}
运行 服务开启了~内置了一个嵌入式的Jetty 运行就是一个服务器了
运行内容:
信息: Creating Service {http://ws.cc.org/}DiscountServiceImplService from class org.cc.ws.DiscountService
2013-5-8 23:54:50 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:80/discount
2013-5-8 23:54:50 org.eclipse.jetty.server.Server doStart
信息: jetty-8.1.7.v20120910
2013-5-8 23:54:50 org.eclipse.jetty.server.AbstractConnector doStart
信息: Started SelectChannelConnector@localhost:80
2013-5-8 23:54:50 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl
2013-5-8 23:54:50 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be soap.udp://239.255.255.250:3702
2013-5-8 23:54:50 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImpl
浏览器输入 http://localhost/discount?wsdl 显示了xml文件并且服务器的console没有报错就正常啦~~~\(≧▽≦)/~
客户端:
使用生成的命令:
wsdl2java -p org.cc.ws.client -d h:\webservice http://localhost/discount?wsdl
将生成好的类放入对应的包里
只需要写一个启动的类即可:
package org.cc.ws.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean();
factoryBean.setAddress("http://localhost/discount");
factoryBean.setServiceClass(DiscountService.class);
DiscountService service=(DiscountService)factoryBean.create();
System.out.println(service.discountPrice(1, 100));
}
}
运行一下:
信息: Creating Service {http://ws.cc.org/}DiscountServiceService from class org.cc.ws.client.DiscountService
90.0
服务器端增加拦截器:
不能使用封装好的发布形式 要自己使用JaxWsServerFactoryBean进行操作:
如下:加入了请求进入和结果送出的拦截器:
package org.cc.ws;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class Server {
public static void main(String[] args) {
// 简单的发布
// 参数一是发布的地址,参数二是服务接口实现类的对象
//Endpoint.publish("http://localhost/discount", new DiscountServiceImpl());
JaxWsServerFactoryBean factory=new JaxWsServerFactoryBean();
factory.setAddress("http://localhost/discount");
// 一定要写实现类 不然可以看到启动是可以的 但是客户端一连接会有:Could not instantiate service class org.cc.ws.DiscountService because it is an interface.
// 如果写错了发生错误改正还是有错误 那么需要重启下eclipse解决
factory.setServiceClass(DiscountServiceImpl.class);
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.create();
}
}
项目结构(额..命名不太规范 但懒得改了..额):
好了 这是第一个运行的项目..接下去的学习会继续