在Web项目中创建CXF服务是我们在项目中最常用的,一般可以分为两类,一个是为类创建CXF服务,一个是为接口创建CXF服务,这篇博文就为大家介绍一下如何实现这两种方式。
一、创建服务端
1.建立web项目
2.引用jar包
3.创建对类和接口
服务类:
-
package com.tgb.cxf.web.server; -
import javax.jws.WebService; -
//服务类 -
@WebService -
public class Hello { -
public String syaHello(String name){ -
return "Hello " +name; -
} -
}
服务接口:
-
package com.tgb.cxf.web.server.impl; -
import javax.jws.WebService; -
//服务接口 -
@WebService -
public interface Bye { -
public String sayBye(String name); -
}
接口实现:
-
package com.tgb.cxf.web.server.impl; -
public class ByeImpl implements Bye{ -
@Override -
public String sayBye(String name) { -
return "Bye " +name; -
} -
}
4.配置web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" -
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"> -
<listener> -
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> -
</listener> -
<!-- 方式一:引入cxf文件 --> -
<context-param> -
<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文件 --> -
<!-- <init-param> -
<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>/service/*</url-pattern> -
</servlet-mapping> -
</web-app>
5.配置cfx.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" /> -
<!-- -
===============配置类的形式webservice服务================= -
address:tomcat的host http://ip:port/projectName/service/后面的一端路径 -
http://ip:port/projectName/service/hello -
implementor:指定具体的服务的类 -
--> -
<jaxws:endpoint id="hello" address="/hello" implementor="com.tgb.cxf.web.server.Hello"> -
<!-- 输入拦截器,打印输入的消息 --> -
<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:endpoint> -
<!-- -
===============配置带有接口的webservice服务================= -
address:tomcat的host http://ip:port/projectName/service/后面的一端路径 -
http://ip:port/projectName/service/bye -
serviceClass:服务接口的类 -
--> -
<jaxws:server address="/bye" serviceClass="com.tgb.cxf.web.server.impl.Bye"> -
<!-- 服务接口的实现类 --> -
<jaxws:serviceBean> -
<bean class="com.tgb.cxf.web.server.impl.ByeImpl"></bean> -
</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中,发布服务
在浏览器中输入项目地址,可以看到发布的服务
二、生成代码
使用wsdl2java命令生成java文件
三、创建客户端
1.引入生成好的服务端代码
2.调用服务类
-
package com.rl.cxf.web.client; -
import com.rl.web.inter.ByeInter; -
import com.rl.web.inter.ByeInterService; -
public class WebInterClient { -
public static void main(String[] args) { -
//调用服务接口 -
ByeInterService bs = new ByeInterService(); -
ByeInter bi = bs.getByeInterPort(); -
String result = bi.sayBye("周周"); -
System.out.println(result); -
} -
}
3.调用服务接口
-
package com.rl.cxf.web.client; -
import com.rl.web.inter.ByeInter; -
import com.rl.web.inter.ByeInterService; -
public class WebInterClient { -
public static void main(String[] args) { -
//调用服务接口 -
ByeInterService bs = new ByeInterService(); -
ByeInter bi = bs.getByeInterPort(); -
String result = bi.sayBye("周周"); -
System.out.println(result); -
} -
}
运行结果如下:
总结:
在web项目中,和java项目不同的是我们使用cfx.xml配置文件,来加载服务工厂对象、加入输入输出拦截器和指定服务类的接口和实现,相比较来说方便了许多,配置也比较简便。