设为首页 收藏本站
查看: 957|回复: 0

[经验分享] Apache cxf JaxWs基本应用

[复制链接]

尚未签到

发表于 2015-11-14 08:22:36 | 显示全部楼层 |阅读模式
  本文以CXF 2.6.x为例,会用到jsr311.jar 。当前CXF最新版本为3.x,依赖jsr版本也有所不同,且Spring配置文件中也不再需要配置:<import resource=&quot;classpath:META-INF/cxf/cxf-extension-soap.xml&quot; />。
  在做版本升级时,需要留以上细节。
  


  现在开始以CXF2.6.x做一些Demo。
  一、首先我们搭建一个Maven Project,其中pom.xml完整内容如下:
  

<project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;>
<modelVersion>4.0.0</modelVersion>
<artifactId>abc-api</artifactId>
<packaging>war</packaging>
<version>${global.version}</version>
<parent>
<groupId>com.abc.module</groupId>
<artifactId>abc-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>stax2-api</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.jbarcode</groupId>
<artifactId>jbarcode</artifactId>
<version>0.2.8</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>
  


  二、配置web.xml
  

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;
id=&quot;WebApp_ID&quot; version=&quot;2.5&quot;>
<display-name>fsp-api</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application.xml</param-value>
</context-param>
<!-- spring context listener -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- CXF -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>


三、创建Webservice对外业务接口  
  
  

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface ILogisticsWsApi {
/**
* @param xmlParam
* @return xml string
*/
@WebMethod(operationName = &quot;itemConfirm&quot;)
public String itemConfirm(@WebParam String xmlParam);
}

四、实现Webservice接口  
  

public class LogisticsWsApiImpl implements ILogisticsWsApi {
private Logger log = LoggerFactory.getLogger(getClass());
@Override
public String itemConfirm(String xmlParam) {
// TODO Auto-generated method stub
log.debug(&quot;itemConfirm xml param : &quot; + xmlParam);
LogisticsMessage logisticsMessage = LogisticsFactory.createItemConfirmRequest();
logisticsMessage.decode(xmlParam);
// to do something ...
return response;
}
}
  
  五、配置Spring xml,让Webservice提供服务
  

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:jaxws=&quot;http://cxf.apache.org/jaxws&quot;
xmlns:jaxrs=&quot;http://cxf.apache.org/jaxrs&quot;
xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd&quot;>
<import resource=&quot;classpath:META-INF/cxf/cxf.xml&quot; />
<import resource=&quot;classpath:META-INF/cxf/cxf-extension-soap.xml&quot; />
<import resource=&quot;classpath:META-INF/cxf/cxf-servlet.xml&quot; />
<bean id=&quot;loggingInInterceptor&quot; class=&quot;org.apache.cxf.interceptor.LoggingInInterceptor&quot;/>
<bean id=&quot;loggingOutInterceptor&quot; class=&quot;org.apache.cxf.interceptor.LoggingOutInterceptor&quot;/>
<jaxws:endpoint id=&quot;logisticsWsApiServiceContainer&quot; implementor=&quot;com.abc.api.service.LogisticsWsApiImpl&quot; address=&quot;/logisticsWsApi&quot; >
<jaxws:inInterceptors>
<ref bean=&quot;loggingInInterceptor&quot;/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean=&quot;loggingOutInterceptor&quot;/>
</jaxws:outInterceptors>
</jaxws:endpoint>
</beans>

  至此,Webservice服务端的代码就完成了,启动Web server即可以对外提供服务了。
  假设你当前的Maven Project名字为:abc-api,那么实际访问Webservice 的Address就为:http://ip:port/abc-api/services/logisticsWsApi?wsdl
  


  六、我们编写一个JunitTester测试前面的Webservice接口
  

import static org.junit.Assert.*;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.abc.warehouse.service.ILogisticsWsApi;
public class LogisticsWsApiTester {
private JaxWsProxyFactoryBean wsFactory;
private String address = &quot;http://localhost:8080/abc-api/services/logisticsWsApi&quot;;
/**
*
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
wsFactory = new JaxWsProxyFactoryBean();
wsFactory.setAddress(address);
wsFactory.setServiceClass(ILogisticsWsApi.class);
}
/**
*
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
wsFactory = null;
}
/**
* Test method for {@link com.abc.api.service.LogisticsWsApiImpl#itemConfirm(java.lang.String)}.
*/
@Test
public void testItemConfirm() {
//        fail(&quot;Not yet implemented&quot;);
String xmlParam = &quot;Hello JaxWs , 欢迎&quot;;
ILogisticsWsApi logisticsWsApi = (ILogisticsWsApi) wsFactory.create();
String recvMessage = logisticsWsApi.itemConfirm(xmlParam);
assertEquals(recvMessage, null);
}

  当然,我们也可以把JaxWsProxyFactoryBean用Spring类配置或直接在Spring中配置jaxws:client
  

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:jaxws=&quot;http://cxf.apache.org/jaxws&quot;
xmlns:jaxrs=&quot;http://cxf.apache.org/jaxrs&quot;
xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd&quot;>
<import resource=&quot;classpath:META-INF/cxf/cxf.xml&quot; />
<import resource=&quot;classpath:META-INF/cxf/cxf-extension-soap.xml&quot; />
<import resource=&quot;classpath:META-INF/cxf/cxf-servlet.xml&quot; />
<jaxws:client id=&quot;logisticsWsApi&quot; serviceClass=&quot;com.abc.warehouse.service.ILogisticsWsApi&quot; address=&quot;${logisticsWsApiAddress}&quot;/>
</beans>

<bean id=&quot;logisticsWsApi&quot; class=&quot;com.abc.warehouse.service.ILogisticsWsApi&quot; factory-bean=&quot;clientFactory&quot; factory-method=&quot;create&quot; />
<bean id=&quot;clientFactory&quot; class=&quot;org.apache.cxf.jaxws.JaxWsProxyFactoryBean&quot;>
<property name=&quot;serviceClass&quot; value=&quot;com.abc.warehouse.service.ILogisticsWsApi&quot; />
<property name=&quot;address&quot; value=&quot;${logisticsWsApiAddress}&quot; /> <!--  http://localhost:8080/abc-api/services/logisticsWsApi  -->
</bean>


  到这里,我们就已经完成了Webservice服务端和客户端的开发。
  其中,在日志拦截器LoggingInInterceptor类的logging(Logger logger, Message message)函数中,在输出日志时可能会遇到乱码问题,这个乱码不会影响实际的业务操作。
  如果要修正这里的乱码,可以通过重载该函数来处理解码中文的问题。修改方法很简单,详情见:《Apache cxf JaxRs基本应用》。
  


  


  


  


  


  


  


  


  


  



版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-138981-1-1.html 上篇帖子: Apache内存池内幕(1) 下篇帖子: Apache Http Server和Tomcat 之区别
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表