cxf webservice 练习

<div class="it610-blog-content-contain" style="font-size: 14px"></div>
cxf的官网文档地址:http://cxf.apache.org/docs/
1.建立一个web project,名称为cfx-new,部署目录写成ROOT
2.下载最新的cxf包http://cxf.apache.org/download.html,我下载的是apache-cxf-2.5.2.zip,直接将lib下的所有jar拷贝到lib下
3.新建一个接口类HelloWorld

Java代码 
1.package info.frady.service;  
2. 
3.import javax.jws.WebParam;  
4.import javax.jws.WebService;  
5. 
6.@WebService 
7.public interface HelloWorld {  
8.    String sayHi(String text);  
9.} 
package info.frady.service;

import javax.jws.WebParam;
import javax.jws.WebService;

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

4.建立一个接口的实现类HelloWorldImpl

Java代码 
1.package info.frady.service.impl;  
2. 
3.import info.frady.service.HelloWorld;  
4. 
5.import javax.jws.WebService;  
6. 
7.@WebService(endpointInterface = "info.frady.service.HelloWorld",   
8.            serviceName = "helloWorld")  
9.public class HelloWorldImpl implements HelloWorld {  
10.   
11.    public String sayHi(String text) {  
12.        return "Hello " + text;  
13.    }  
14.} 
package info.frady.service.impl;

import info.frady.service.HelloWorld;

import javax.jws.WebService;

@WebService(endpointInterface = "info.frady.service.HelloWorld",
            serviceName = "helloWorld")
public class HelloWorldImpl implements HelloWorld {

    public String sayHi(String text) {
        return "Hello " + text;
    }
}

说明下:
*@WebService:申明为webservice的注解,这样cxf就知道这是个webservice了
*endpointInterface:要暴露的接口类
*serviceName :    服务名,其实就是调用地址
5.在WEB-INF目录下新建beans.xml,如下:

Java代码 
1.<?xml version="1.0" encoding="UTF-8"?>  
2.<!-- START SNIPPET: beans -->  
3.<beans xmlns="http://www.springframework.org/schema/beans" 
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
5.    xmlns:jaxws="http://cxf.apache.org/jaxws" 
6.    xsi:schemaLocation="  
7.http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
8.http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
9. 
10.    <import resource="classpath:META-INF/cxf/cxf.xml" />  
11.    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
12.    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
13.    <jaxws:endpoint   
14.      id="helloWorld"   
15.      implementor="info.frady.service.impl.HelloWorldImpl"   
16.      address="/helloWorld" />  
17.</beans> 
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<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" />
<jaxws:endpoint
  id="helloWorld"
  implementor="info.frady.service.impl.HelloWorldImpl"
  address="/helloWorld" />
</beans>

说明:
注: implementor :接口类的实现类
        address:   要和注释里面声明的服务名对应,对应的是外部访问地址
6.在修改web.xml文件,如下

Java代码 
1.<?xml version="1.0" encoding="ISO-8859-1"?>  
2. 
3.<!DOCTYPE web-app  
4.    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
5.    "http://java.sun.com/dtd/web-app_2_3.dtd">  
6. 
7.<!-- START SNIPPET: webxml -->  
8.<web-app>  
9.    <context-param>  
10.        <param-name>contextConfigLocation</param-name>  
11.        <param-value>/WEB-INF/beans.xml</param-value>  
12.    </context-param>  
13. 
14.    <listener>  
15.        <listener-class>  
16.            org.springframework.web.context.ContextLoaderListener  
17.        </listener-class>  
18.    </listener>  
19. 
20.    <servlet>  
21.        <servlet-name>CXFServlet</servlet-name>  
22.        <display-name>CXF Servlet</display-name>  
23.        <servlet-class>  
24.            org.apache.cxf.transport.servlet.CXFServlet  
25.        </servlet-class>  
26.        <load-on-startup>1</load-on-startup>  
27.    </servlet>  
28. 
29.    <servlet-mapping>  
30.        <servlet-name>CXFServlet</servlet-name>  
31.        <url-pattern>/services/*</url-pattern>  
32.    </servlet-mapping>  
33.</web-app>  
34.<!-- END SNIPPET: webxml --> 
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<!-- START SNIPPET: webxml -->
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</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>
</web-app>
<!-- END SNIPPET: webxml -->


7.启动tomcat
8.输入地址http://localhost:9081/services/,出现WSDL : {http://impl.service.frady.info/}HelloWorld说明服务端成功。
9.编写测试代码

Java代码 
1.import info.frady.domain.Person;  
2.import info.frady.service.HelloWorld;  
3. 
4.import org.apache.cxf.interceptor.LoggingInInterceptor;  
5.import org.apache.cxf.interceptor.LoggingOutInterceptor;  
6.import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
7. 
8. 
9.public class TestHello {  
10. 
11.    /** 
12.     * @param args 
13.     */ 
14.    public static void main(String[] args) {  
15.        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
16.        factory.getInInterceptors().add(new LoggingInInterceptor());  
17.        factory.getOutInterceptors().add(new LoggingOutInterceptor());  
18.        factory.setServiceClass(HelloWorld.class);  
19.        factory.setAddress("http://localhost:8080/services/HelloWorld");  
20.        HelloWorld client = (HelloWorld) factory.create();  
21.        String reply = client.sayHi("frady");  
22.        System.out.println("Server said: " + reply);      
23.        System.exit(0);   
24.    }  
25. 
26.} 

你可能感兴趣的:(我是菜鸟)