首页 > 其他分享 >Apache CXF使用入门

Apache CXF使用入门

时间:2022-11-19 18:32:42浏览次数:117  
标签:xml cxf http 入门 text application CXF Apache org

想玩玩Apache CXF框架,以前一直在用xfire,很想知道这个xfire的升级版本怎么样,折腾了一通,在这里记下配置过程。

1.下载apache cxf框架

     是人都知道去官方网站去下载就行了。

2.配置过程

2.1 web.xml文件

配置为以下内容即可:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app>

 <display-name>WebServicesClient</display-name>  <description>Hebbn WebServices Client</description>

 <context-param>   <param-name>webAppRootKey</param-name>   <param-value>HebbnClient.root</param-value>  </context-param>  <context-param>   <param-name>log4jConfigLocation</param-name>   <param-value>/WEB-INF/log4j.properties</param-value>  </context-param>  <context-param>   <param-name>log4jRefreshInterval</param-name>   <param-value>60000</param-value>  </context-param>  <context-param>   <param-name>contextConfigLocation</param-name>   <param-value>    /WEB-INF/applicationContext.xml   </param-value>  </context-param>

 <listener>   <listener-class>    org.springframework.web.context.ContextLoaderListener   </listener-class>  </listener>  <listener>   <listener-class>    org.springframework.web.util.IntrospectorCleanupListener   </listener-class>  </listener>  <listener>   <listener-class>    org.springframework.web.context.request.RequestContextListener   </listener-class>  </listener>  <listener>   <listener-class>    org.springframework.web.util.Log4jConfigListener   </listener-class>  </listener>

 <servlet>   <servlet-name>CXFServlet</servlet-name>   <display-name>CXF Servlet</display-name>   <servlet-class>    org.apache.cxf.transport.servlet.CXFServlet   </servlet-class>   <load-on-startup>1</load-on-startup>  </servlet>

 <servlet-mapping>   <servlet-name>CXFServlet</servlet-name>   <url-pattern>/services/*</url-pattern>  </servlet-mapping>  <session-config>   <session-timeout>30</session-timeout>  </session-config>

 <!-- MIME mapping -->  <mime-mapping>   <extension>inc</extension>   <mime-type>text/plain</mime-type>  </mime-mapping>

 <mime-mapping>   <extension>doc</extension>   <mime-type>application/vnd.ms-word</mime-type>  </mime-mapping>  <mime-mapping>   <extension>gif</extension>   <mime-type>image/gif</mime-type>  </mime-mapping>  <mime-mapping>   <extension>htm</extension>   <mime-type>text/html</mime-type>  </mime-mapping>  <mime-mapping>   <extension>html</extension>   <mime-type>text/html</mime-type>  </mime-mapping>  <mime-mapping>   <extension>jad</extension>   <mime-type>text/vnd.sun.j2me.app-descriptor</mime-type>  </mime-mapping>  <mime-mapping>   <extension>jnlp</extension>   <mime-type>application/x-java-jnlp-file</mime-type>  </mime-mapping>  <mime-mapping>   <extension>jpeg</extension>   <mime-type>image/jpeg</mime-type>  </mime-mapping>  <mime-mapping>   <extension>jpg</extension>   <mime-type>image/jpeg</mime-type>  </mime-mapping>  <mime-mapping>   <extension>js</extension>   <mime-type>application/x-javascript</mime-type>  </mime-mapping>  <mime-mapping>   <extension>pdf</extension>   <mime-type>application/pdf</mime-type>  </mime-mapping>  <mime-mapping>   <extension>png</extension>   <mime-type>image/png</mime-type>  </mime-mapping>  <mime-mapping>   <extension>rar</extension>   <mime-type>application/x-rar-compressed</mime-type>  </mime-mapping>  <mime-mapping>   <extension>txt</extension>   <mime-type>text/plain</mime-type>  </mime-mapping>  <mime-mapping>   <extension>xls</extension>   <mime-type>application/vnd.ms-excel</mime-type>  </mime-mapping>  <mime-mapping>   <extension>xml</extension>   <mime-type>text/xml</mime-type>  </mime-mapping>  <mime-mapping>   <extension>xul</extension>   <mime-type>application/vnd.mozilla.xul-xml</mime-type>  </mime-mapping>  <mime-mapping>   <extension>zhtml</extension>   <mime-type>text/html</mime-type>  </mime-mapping>  <mime-mapping>   <extension>zip</extension>   <mime-type>application/x-zip</mime-type>  </mime-mapping>  <mime-mapping>   <extension>zul</extension>   <mime-type>text/html</mime-type>  </mime-mapping>  <mime-mapping>   <extension>wsdl</extension>   <mime-type>text/xml</mime-type>  </mime-mapping>

 <mime-mapping>   <extension>xsd</extension>   <mime-type>text/xml</mime-type>  </mime-mapping>  <welcome-file-list>   <welcome-file>index.jsp</welcome-file>  </welcome-file-list> </web-app>

主要的就是apace cxf那个servlet的配置参数以及spring的一些配置参数要搞对就行了。

2.2 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"  xsi:schemaLocation=" 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">  <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" />

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   <property name="locations">    <list>     <value>WEB-INF/jdbc.properties</value>    </list>   </property>  </bean>

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   <property name="driverClassName" value="${jdbc.driverClassName}"/>   <property name="url" value="${jdbc.url}"/>   <property name="username" value="${jdbc.username}"/>   <property name="password" value="${jdbc.password}"/>   <property name="maxActive" value="${jdbc.maxActive}"/>   <property name="initialSize" value="${jdbc.initialSize}"/>   <property name="maxWait" value="${jdbc.maxWait}"/>   <property name="minIdle" value="${jdbc.minIdle}"/>   <property name="testOnBorrow" value="true"/>   <property name="testOnReturn" value="true"/>   <property name="testWhileIdle" value="true"/>   <property name="validationQuery" value="select 1 from dual"/>  </bean>

 <!-- Transaction manager for a single JDBC DataSource -->  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   <property name="dataSource" ref="dataSource"/>  </bean>

 <jaxws:endpoint id="testServices" implementor="com.tsxd.hebbn.services.TestServices" address="/testServices" />   </beans>

如果要操作数据库的话就带着配置参数中数据库相关的部分,但是在apace cxf的下载包中不包含数据库相关的spring库文件,需要自己下载。单纯的想测试Apache CXF的话就留着最后一行就得了。

2.3 Java测试代码

2.3.1 接口类源代码

注意那个@WebService

package com.tsxd.hebbn.services;

import javax.jws.WebService; @WebService public interface ITestServices {

   public java.util.Date getServerDate();    public String sayHello(String name); }

2.3.2 实现类代码

package com.tsxd.hebbn.services;

import java.util.Date;

import javax.jws.WebService;

@WebService(endpointInterface="com.tsxd.hebbn.services.ITestServices") public class TestServices implements ITestServices {

 public Date getServerDate() {   return new Date();  }

 public String sayHello(String name) {   if (null==name){    return "fuck u";   }   return "hello :"+name;  }

}

也是注意那个@WebService声明。

2.4 测试

部署到tomcat中去,因为在web.xml文件中配置了cxf servlet 类mapping到/services路径,所以访问时

http://IP地址:端口号/应用程序上下文/services/就行了

应该会有已经配置好的services的列表,像下边这样:

{http://services.hebbn.tsxd.com/}TestServicesPort

点击联接可以查看WSDL描述

2.5 最后

    这个破Apache CXF和spring绑的也TMD太紧密了,用spring的话就会造成生成的wsdl描述中参数名称都变成args0这样无意义的名称,不好,不喜欢,结论,还是直接用xfire就行了,没必要用这个巨大的CXF.

 

 

 

标签:xml,cxf,http,入门,text,application,CXF,Apache,org
From: https://blog.51cto.com/u_15883582/5870534

相关文章