欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CXF spring实例 博客分类: SOA webservicespringcxf

程序员文章站 2024-03-22 20:08:46
...
CXF spring实例

1、服务器端



package cxf;
import javax.jws.WebService;

@WebService
public interface  HelloWorld {
  public String sayHello(String text) ;
}

package cxf;

import javax.jws.WebService;

@WebService  
public class HelloWorldImpl implements HelloWorld {   
  public String sayHello(String text) {   
         return "55555555555555555555555" + text ;   
}   

}

spring-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"
    xsi:schemaLocation=" 
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd 
http://cxf.apache.org/jaxwshttp://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="hello" class="cxf.HelloWorldImpl" />  
    <jaxws:endpoint id="helloWorld" implementor="cxf.HelloWorldImpl"  
        address="/ws" />  
  </beans>

web.xml

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

  <context-param>  
        <param-name>contextConfigLocation</param-name>   
        <param-value>/WEB-INF/spring-cxf.xml</param-value>  
    </context-param>  
  
    <listener>  
        <listener-class>  
         org.springframework.web.context.ContextLoaderListener  
        </listener-class>  
    </listener>  
        
    <servlet>  
        <servlet-name>CXFServlet</servlet-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>/*</url-pattern>  
    </servlet-mapping>
      <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list> 
</web-app>
2、客户端

package cxf;
import javax.jws.WebService;

@WebService
public interface  HelloWorld {
   public String sayHello(String text);

}

package cxf;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {

/**
  * @param args
  */
public static void main(String[] args) {
  System.out.println(System.getProperty("java.endorsed.dirs"));
  ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-cxf.xml");
      
   HelloWorld client = (HelloWorld) ctx.getBean("cxfClient");   
         String result = client.sayHello("你好!");   
         System.out.println(result);   
}

}



spring-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"
    xsi:schemaLocation=" 
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd 
http://cxf.apache.org/jaxwshttp://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="cxfClient" class="cxf.HelloWorld"
        factory-bean="clientFactory" factory-method="create" />
  
<bean id="clientFactory"   class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="cxf.HelloWorld" />
        <property name="address" value="http://localhost:8080/ws" />
    </bean>
    </beans>
web.xml

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

  <context-param>  
        <param-name>contextConfigLocation</param-name>   
        <param-value>/WEB-INF/spring-cxf.xml</param-value>  
    </context-param>  
  
    <listener>  
        <listener-class>  
         org.springframework.web.context.ContextLoaderListener  
        </listener-class>  
    </listener>  
        
    <servlet>  
        <servlet-name>CXFServlet</servlet-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>/*</url-pattern>  
    </servlet-mapping>
      <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list> 
</web-app>





注意:

1、发生如下错误时候

nested exception is java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader, but this RI needs 2.1 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (See http://java.sun.com/j2se/1.5.0/docs/guide/standards/)

解决方法:

System.out.println(System.getProperty("java.endorsed.dirs"));能取得存放目录

将jaxb-api.jar包放到%{JAVA_HOME}\jre\lib\endorsed下即可,如果没有endorsed,则新建一个.这两个jar包我放提供在附件里

2、javamail异常:java.lang.NoClassDefFoundError: com/sun/activation/registries/LogSupport

解决方法:

D:\Program Files\MyEclipse 6.0\myeclipse\eclipse\plugins\com.genuitec.eclipse.j2eedt.core_6.0.1.zmyeclipse601200710\data\libraryset\EE_5
javaee.jar包中的 mail和 activation
我将着两项删除了,我使用的是javaee5

问题被解决了