WebService之CXF框架应用后篇

一、CXF框架发布WebService服务

1、所需资源:apache-cxf-2.4.2.zip、cxf环境和jdk环境、Eclipse开发平台

2、建Dynamic Web Project,将apache-cxf-2.4.2.zip解压出来的lib文件夹的jar包加入工程,注意其中有个WHICH_JARS文件不是jar包!

3、创建接口服务类

package top.einino.cxf.server;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
@WebService(

serviceName=”MyServiceName”,//修改服务访问点集合名字
portName=”MyHelloServer”,//修改端口名字
name=”MyHelloServer”,//修改服务类的名字
targetNamespace=”top.einino.ws” //修改命名空间名字

)

//改变soap版本,默认为11
@BindingType(value=javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)

public interface HelloServer {

public  @WebResult(name=”helloresult”) String sayHello(@WebParam(name=”personName”) String name);

}

 

4、创建接口实现类

package top.einino.cxf.server;

public class HelloServerImpl implements HelloServer{

@Override
public String sayHello(String name) {

return name+” hello”;

}

}

5、编辑web.xml文件,配置CXFServlet类,访问服务类,用spring加载配置文件cxf.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://java.sun.com/xml/ns/javaee” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=”WebApp_ID” version=”2.5″>

<!– spring监听器 –>
<listener>

<!– spring-web.jar包下的context –>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!– 第二种配置cxf.xml文件的方法cxf.xml本来就是spring的配置文件,所以可以用spring的监听器来加载这个配置文件 –>
<context-param>

<!– contextConfigLocation等于ContextLoaderListener里的父类的一个变量 –>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cxf.xml</param-value>

</context-param>

<servlet>

<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!–  第一种配置cxf.xml文件的方法<init-param> –>
<!– config-location是得看下CXFServlet里的原码来配置的 –>
<!–   <param-name>config-location</param-name>
<param-value>classpath:cxf.xml</param-value>
</init-param> –>
<!– 随着服务器启动时加载 –>
<!–  <load-on-startup>1</load-on-startup> –>

</servlet>

<servlet-mapping>

<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>

</servlet-mapping>

</web-app>

6、新建cxf.xml文件

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:jaxws=”http://cxf.apache.org/jaxws”
xmlns:jaxrs=”http://cxf.apache.org/jaxrs” xmlns:cxf=”http://cxf.apache.org/core”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd”>

<!– 引入CXF Bean定义如下,为了兼容早期的版本中使用 –>
<!– 在cxf-2.4.2.jar包可找到 –>
<import resource=”classpath:META-INF/cxf/cxf.xml” />
<import resource=”classpath:META-INF/cxf/cxf-extension-soap.xml” />
<import resource=”classpath:META-INF/cxf/cxf-servlet.xml” />

<!–
===============配置带有接口的webservice服务=================
address:tomcat服务器下访问项目的路径+(web.xml配置下的)/services/+address
http://localhost:8080/ws_cxf_web_server/services/hello?wsdl
serviceClass:指定服务接口类

–>

<jaxws:server id=”hello” address=”/hello” serviceClass=”top.einino.cxf.server.HelloServer“>
<jaxws:serviceBean>
<bean class=”top.einino.cxf.server.HelloServerImpl“/>
</jaxws:serviceBean>
<jaxws:inInterceptors>
<bean class=”org.apache.cxf.interceptor.LoggingInInterceptor“></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class=”org.apache.cxf.interceptor.LoggingOutInterceptor“></bean>
</jaxws:outInterceptors>
</jaxws:server>

</beans>

6、启动tomcat服务器,即发布服务,通过浏览器访问http://localhost:8080/ws_cxf_web_server/services/hello?wsdl即可得到服务说明文件

二、CXF框架生成客户端代码

1、执行wsdl2java命令

命令具体语句:wsdl2java -d . -p top.einino.cxf.client http://127.0.0.1:1128/hello?wsdl

2、创建工程将生成的代码导入,调用即可

package top.einino.client;

import top.einino.cxf.client.MyHelloServer;
import top.einino.cxf.client.MyServiceName;

public class HelloClient {

public static void main(String[] args) {

//创建服务集合点,方式跟jdk调用基本相同
MyServiceName myServiceName = new MyServiceName();
//得到服务实现类
MyHelloServer myHelloServer = myServiceName.getMyHelloServer();
//调用业务逻辑
String sayHello = myHelloServer.sayHello(“BINGO”);
//测试数据
System.out.println(sayHello);

}

}

三、小结

该博文主要介绍cxf框架在web的应用,通过在web.xml配置CXFServelt来访问服务,并用wsdl2java命令生成客户端代码来调用

如果有疑问或者对该文章有何看法或建议或有问题的,欢迎评论,恳请指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值