开发cxf的web项目-第一种发布方式(没有借口的发布方式)

l由于cxfweb项目已经集成了Spring所以,cxf的服务类都是在spring的配置文件中完成的。以下是步骤:

l第一步:建立一个web项目。
l第二步:准备所有jar包。将cxf_home\lib项目下的所有jar包全部copy到新项目的lib目录下,里面已经包含了spring3.0jar包。
l第三步:在web.xml中配置cxf的核心servletCXFServlet

主要写<servlet></servlet>;<servlet-mapping></servlet-mapping>
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>CXF_3</display-name>
  <!-- 配置cxf框架的核心Servlet -->
  <servlet>
  	  <servlet-name>cxf</servlet-name>
  	  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  		<servlet-name>cxf</servlet-name>
  		<url-pattern>/cxf/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


第四步:在web-inf下创建(最好是Copy其他的,防止出错)cxf-servlet.xml文件。这是一个spring的配置文件

<?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定义如下,早期的版本中使用 -->
	<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" />

	<!-- 第一张发布方式简单发布方式(没有接口的发布方式) -->
	<!-- id:唯一性标示  implementor:提供服务的类  adress:服务的请求URL-->
	<jaxws:endpoint id = "helloService" implementor= "cn.itcast.cxf.HelloService" address = "/hello">
	
	</jaxws:endpoint>
</beans>

xml文件中只需配置<jaxws:endpoint></ jaxws:endpoint>即可;

下面将项目发布到tomcat上即可

下面通过浏览器访问wsdl文件,http://localhost:8080/CXF_3/cxf/hello?wsdl,注意端口号为8080,不是8009,否则会出错

也可以访问:http://localhost:8080/CXF_3/cxf/,点击wsdl地址即可


但是可以看到wsdl文件中没有显示对应的方法,所以需要在方法上加注解,如下

package cn.itcast.cxf;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
@WebService
@BindingType(value=SOAPBinding.SOAP12HTTP_BINDING)
public class HelloService {

	/**
	 * @param args
	 */
		
		public String sayHello(String name) {
			
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			
			System.out.println("sayHello...."+name);
			
			return sdf.format(new Date())+ " hello: " +name;
		}

}
之后,生成客户端代码调动服务

这里我们可以用wsdl2java来生成客户端代码,执行命令为:wsdl2java -d . -p com.cxf.client http://localhost:8080/CXF_3/cxf/hello?wsdl;

需要注意的是用wsdl2java生成的客户端代码是根据jdk1.7生成的,若环境未jdk1.6会报错,这里只需要把报错的代码中第二个参数删除即可,

如下图:


最后

package com.cxf.client;

public class App {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HelloServiceService hss = new HelloServiceService();
		
		HelloService hs =	hss.getHelloServicePort();
		
		String ret = hs.sayHello("aa");
		System.out.println(ret);
	}

}

创建java类调用一下就可以了,代码如下:


如果需要加入拦截器,我们可以直接在cxf-servlet.xml文件中配置,不需要向之前那样在服务端代码中修改代码,如修改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定义如下,早期的版本中使用 -->
	<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" />

	<!-- 第一张发布方式简单发布方式(没有接口的发布方式) -->
	<!-- id:唯一性标示  implementor:提供服务的类  adress:服务的请求URL-->
	<jaxws:endpoint id = "helloService" implementor= "cn.itcast.cxf.HelloService" address = "/hello">
	    <!--  -->
		<jaxws:inInterceptors>
		    <!--加入请求的消息拦截器  -->
			<bean class = "org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
			<!-- 加入响应的消息拦截器 -->
			<bean class = "org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
		</jaxws:inInterceptors>
	</jaxws:endpoint>
</beans>

至此,第一种发布方式结束


最后,我们还有一个问题就是,我们只在项目中创建了一个cxf-servlet.xml文件,并没有在其他地方进行配置,是怎么找到对应的xml文件,并调用的呢?

这个问题我们可以看下web.xml,在xml文件中,我没配置了相应的servlet,有一段代码为:

<servlet>
  	  <servlet-name>cxf</servlet-name>
  	  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>ApplicationContext

我们可以对
org.apache.cxf.transport.servlet.CXFServle类进行反编译,查看源码,解析的代码如下
<img src="https://img-blog.csdn.net/20150820112848548?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
可以看到如果没有配置的话,系统会默认读取/WEB-INF/cxf-servlet.xml,因此不需要配置;
那么如何让其读取指定目录下的xml文件呢,我们可以在web.xml中配置,加入初始化参数,
我们先将cxf-servlet.xml文件移到src目录下,这时如果不加初始化参数,肯定读不到xml文件的,因此我们要加入初始化参数,web.xml代码如下:
<pre name="code" class="html"><?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>CXF_3</display-name>
  <!-- 配置cxf框架的核心Servlet -->
  <servlet>
  	  <servlet-name>cxf</servlet-name>
  	  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  	  <!-- 通过初始化参数配置cxf文件的位置 -->
  	  <init-param>
  	  	<param-name>config-location</param-name>
  	  	<param-value>classpath:cxf-servlet.xml</param-value>
  	  </init-param>
  </servlet>
  <servlet-mapping>
  		<servlet-name>cxf</servlet-name>
  		<url-pattern>/cxf/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


 
其中,
          <init-param>
  <span style="white-space:pre">	</span>  <span style="white-space:pre">	</span><param-name>config-location</param-name>
  <span style="white-space:pre">	</span>  <span style="white-space:pre">	</span><param-value>classpath:cxf-servlet.xml</param-value>
  <span style="white-space:pre">	</span>  </init-param>
为初始化参数,config-location为固定参数名,param-value为对应的servlet.xml文件名
这样就可以读取到对应的servlet.xml文件了
 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值