首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

tomcat中公布CXF程序

2012-06-29 
tomcat中发布CXF程序Tomcat下发布CXF实现的WebService1、拷贝整个工程到tomcat的webapps下。2、放入后把需要

tomcat中发布CXF程序
Tomcat下发布CXF实现的WebService

1、拷贝整个工程到tomcat的webapps下。
2、放入后把需要的包都考入到lib目录中。
3、在WEB-INF下添加两个文件,分别是beans.xml和web.xml
   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/beans.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>

     </servlet>

     <servlet-mapping>

         <servlet-name>CXFServlet</servlet-name>

         <url-pattern>/services/*</url-pattern>

     </servlet-mapping>

</web-app>

   beans.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/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="webServiceHelloWorld" address="/HH" implementor="com.cxf.impl.Server"/>
<jaxws:endpoint id="Test" address="/Test" implementor="cxf.test.HelloWorldImpl"/>
</beans>

4、其中服务器端的代码如下:
(1)第一个服务器程序
IhelloWorldService接口:
package com.cxf.interfaces;

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

@WebService
public interface IHelloWorldService {
   
    String sayHello(@WebParam(name="username") String username);
   
}
Server类:
package com.cxf.impl;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import com.cxf.interfaces.IHelloWorldService;

@WebService(endpointInterface="com.cxf.interfaces.IHelloWorldService",serviceName="helloWorldService",targetNamespace="http://interfaces.cxf.com/")
public class Server implements IHelloWorldService{

    public String sayHello(String username) {
        return "Hello,"+username;
    }
    public static void main(String[] args) {
        Server impl=new Server();
        String address="http://127.0.0.1:9000/hello";
        Endpoint.publish(address, impl);
    }
}

(2)第二个服务器程序
接口:
package cxf.test;

import javax.jws.WebService;

@WebService

public interface HelloWorld

{

  // 一个简单的方法,返回一个字符串

String say(String hello);

}

类:
package cxf.test;

import javax.jws.WebService;  

// WebService实现类. 

// 使用@WebService指向Interface定义类即可. 

@WebService(endpointInterface = "cxf.test.HelloWorld")  

public class HelloWorldImpl implements HelloWorld  

{  

    public String say(String hello)  

    {  

        return "hello " + hello+"!";  

    }  

}





5、查看,在IE中输入:http://localhost/CXF/services/HH?wsdl,或者http://localhost/CXF/services/Test?wsdl,其中CXF为放入webapps中的文件夹。

6、客户端程序:
(1)
package com.cxf.client;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class ClientFromWsdl {
   

    public static void main(String[] args) {
    try
    {
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost/CXF/services/HH?wsdl");
            //sayHello 为接口中定义的方法名称   张三为传递的参数   返回一个Object数组
    Object[] objects=client.invoke("sayHello", "张三");   
    //输出调用结果
    System.out.println(objects[0].toString());
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
}

(2)
package cxf.test;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class ClientFromWsdl {
   

    public static void main(String[] args) {
    try
    {
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost/CXF/services/Test?wsdl");
            //sayHello 为接口中定义的方法名称   张三为传递的参数   返回一个Object数组
    Object[] objects=client.invoke("say", "李四");   
    //输出调用结果
    System.out.println(objects[0].toString());
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
}



热点排行