Apache cxf 2.6.1 最小配置

当前位置:

Apache cxf 2.6.1 最小配置

作者:邓华锋于2013-4-6发布在 分类:java / 阅读96023 次 / 共有0条评论  
首先去Apache网站下载CXF 最新版本apache-cxf-2.6.1.zip,

添加必须的jar:

cxf-2.6.1.jar 
jetty-continuation-7.5.4.v20111024.jar
jetty-http-7.5.4.v20111024.jar
jetty-io-7.5.4.v20111024.jar
jetty-server-7.5.4.v20111024.jar
jetty-util-7.5.4.v20111024.jar
neethi-3.0.2.jar
wsdl4j-1.6.2.jar
xmlschema-core-2.0.2.jar

这就是CXF-2.6.1的最小配置,以上jar包缺一不可。

下面做个demo测试下。

1.创建一个接口

package com.denghuafeng.service;

public interface HelloWorldService {
    String sayHello(String username);
}


2.创建该接口的实现类
package com.denghuafeng.service;

public class HelloWorldServiceImpl implements HelloWorldService {

    public String sayHello(String username) {
        return "Hello,"+username;
    }
}


3.发布webservice

package com.denghuafeng.server;

import org.apache.cxf.frontend.ServerFactoryBean;
import com.denghuafeng.webservice.HelloWorldService;
import com.denghuafeng.webservice.HelloWorldServiceImpl;

public class Server {
 
  public static void main(String[] args) {
      HelloWorldServiceImpl worldServiceImpl = new HelloWorldServiceImpl();
      ServerFactoryBean factoryBean = new ServerFactoryBean();
      factoryBean.setAddress("http://localhost:8080/hello");
      factoryBean.setServiceClass(HelloWorldService.class);
      factoryBean.setServiceBean(worldServiceImpl);
      factoryBean.create();
 }
}


4.运行Server,在控制台会打印如下信息:

2012-7-2 23:07:07 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://webservice.denghuafeng.com/}HelloWorldService from class com.denghuafeng.webservice.HelloWorldService
2012-7-2 23:07:07 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:8080/hello
2012-07-02 23:07:08.031:INFO:oejs.Server:jetty-7.5.4.v20111024
2012-07-02 23:07:08.187:INFO:oejs.AbstractConnector:Started SelectChannelConnector@localhost:8080 STARTING
2012-07-02 23:07:08.343:INFO:oejsh.ContextHandler:started o.e.j.s.h.ContextHandler{,null}
可试着访问http://localhost:8080/hello?wsdl,看成功没,访问后会看见以下信息:

  <?xml version="1.0" ?>
- <wsdl:definitions name="HelloWorldService" targetNamespace="http://webservice.denghuafeng.com/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.denghuafeng.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://webservice.denghuafeng.com/" xmlns:tns="http://webservice.denghuafeng.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="sayHello" type="tns:sayHello" />
- <xsd:complexType name="sayHello">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="arg0" type="xsd:string" />
  </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="sayHelloResponse" type="tns:sayHelloResponse" />
- <xsd:complexType name="sayHelloResponse">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="return" type="xsd:string" />
  </xsd:sequence>
  </xsd:complexType>
  </xsd:schema>
  </wsdl:types>
- <wsdl:message name="sayHelloResponse">
  <wsdl:part element="tns:sayHelloResponse" name="parameters" />
  </wsdl:message>
- <wsdl:message name="sayHello">
  <wsdl:part element="tns:sayHello" name="parameters" />
  </wsdl:message>
- <wsdl:portType name="HelloWorldServicePortType">
- <wsdl:operation name="sayHello">
  <wsdl:input message="tns:sayHello" name="sayHello" />
  <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" />
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="HelloWorldServiceSoapBinding" type="tns:HelloWorldServicePortType">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="sayHello">
  <soap:operation soapAction="" style="document" />
- <wsdl:input name="sayHello">
  <soap:body use="literal" />
  </wsdl:input>
- <wsdl:output name="sayHelloResponse">
  <soap:body use="literal" />
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="HelloWorldService">
- <wsdl:port binding="tns:HelloWorldServiceSoapBinding" name="HelloWorldServicePort">
  <soap:address location="http://localhost:8080/hello" />
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>
5.不关闭Server,接着做客户端调用:

package com.denghuafeng.server;

import org.apache.cxf.frontend.ClientProxyFactoryBean;

import com.denghuafeng.webservice.HelloWorldService;

public class client {
	public static void main(String[] args){
		ClientProxyFactoryBean cpfb=new ClientProxyFactoryBean();
		cpfb.setAddress("http://localhost:8080/hello");
		cpfb.setServiceClass(HelloWorldService.class);
		HelloWorldService hwcs=(HelloWorldService)cpfb.create();
		System.out.println(hwcs.sayHello("张三"));
	}
}


6.运行Client代码,控制台打印如下信息:
2012-7-2 23:16:45 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://webservice.denghuafeng.com/}HelloWorldService from class com.denghuafeng.webservice.HelloWorldService
Hello,张三

 

 到此,webservice已经成功调用了,证明以上列出的jar包是cxf-2.6.1最小的配置了(注:本示例是在JDK1.6环境下测试运行的)。

  • 标签: 配置 cxf Apache 最小 jar包
  • 该日志于 2013-4-6 14:01 由 邓华锋 发表在 邓华锋 上,你除了可以发表评论外,还可以转载"Apache cxf 2.6.1 最小配置"日志到你的网站或博客,但是请保留源地址及作者信息,谢谢!!
  • 版权所有:《邓华锋》 => 《Apache cxf 2.6.1 最小配置
    本文地址:http://dhf.ink/post-169.html
    除非注明,文章均为 《邓华锋》 原创,欢迎转载!转载请注明本文地址,谢谢。