`
kingaragorn
  • 浏览: 115042 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

CXF学习(1) 使用CXF实现简单的HelloWorld

阅读更多
项目要用到cxf,对WebService这方面的东西不熟悉,甚至都没有用过!从JavaEye上看到一位大哥把自己录制的视频,甚是高兴,学习一下,补充一下自己!

视频分为7部分,我挨着顺序给写到播客上,希望能够帮助那些想学习cxf又无从下手的朋友!

1. 使用cxf实现简单的HelloWorld

1)使用myeclipse新建一个Java项目,项目名HelloWorld。
2)把下载的cxf-2.3.1的lib下的所有jar文件添加进来,有可能有些文件用不到。
3)建立WebService接口,Package:test,Name:HelloWorld
package test;

public interface HelloWorld {
	public String sayHello(String name);
}

4)建立接口的实现类HelloWorldImpl
package test;

public class HelloWorldImpl implements HelloWorld {

	@Override
	public String sayHello(String name) {
        //为了表明这个方法确实被调用了,写一个输出语句
		System.out.println("Say Hello is called");
		return "Hello " + name;
	}
}

5)为了把接口和实现发布为WebService,我们需要添加一些标注,最简单的是只标注一个@WebService就行了,别的采用默认,后面再讲
package test;

import javax.jws.WebService;

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

package test;

import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements HelloWorld {

	@Override
	public String sayHello(String name) {
		//为了表明这个方法确实被调用了,写一个输出语句
		System.out.println("Say Hello is called");
		return "Hello " + name;
	}
}

6)接下来建立一个类,用来启动我们的WebService。因为cxf内嵌了一个Jetty,很容易通过Jetty把我们的WebService发布出来。
(1)新建一个工厂
(2)为工厂设置WebService具体实现类,注意是具体实现类
(3)为WebService设置地址
(4)只要通过这样的设置,就能够通过Jetty把我们的WebService发布到这个地址
(5)得到Server
(6)启动Server

package test;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class MainServer {

	public static void main(String[] args) {
		JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
		factory.setServiceClass(HelloWorldImpl.class);
		factory.setAddress("http://localhost:8080/HelloWorld");
		
		Server server = factory.create();
		server.start();
	}
}


7)打开浏览器,输入http://localhost:8080/HelloWorld?wsdl,就可以看到WebService生成的WSDL
该 XML 文件并未包含任何关联的样式信息。文档树显示如下。
      
−
<wsdl:definitions name="HelloWorldImplService" targetNamespace="http://test/">
−
<wsdl:types>
−
<xs:schema elementFormDefault="unqualified" targetNamespace="http://test/" version="1.0">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
−
<xs:complexType name="sayHello">
−
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
−
<xs:complexType name="sayHelloResponse">
−
<xs:sequence>
<xs:element minOccurs="0" name="sayHelloResult" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
−
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters">
    </wsdl:part>
</wsdl:message>
−
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters">
    </wsdl:part>
</wsdl:message>
−
<wsdl:portType name="HelloWorld">
−
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
    </wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
    </wsdl:output>
</wsdl:operation>
</wsdl:portType>
−
<wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld">
<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="HelloWorldImplService">
−
<wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:8080/HelloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

8)可以看到WebService已经发布成功。
9)接下来为WebService建立一个客户端,新建一个类HelloWorldClient
(1)新建一个Jax代理工厂
(2)为WebService设置地址
(3)设置接口的类型,因为不知道具体类是什么
(4)通过create方法得到HelloWorld
(5)调用HelloWorld方法

10)启动服务器,然后运行HelloWorld的客户端
package test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class HelloWorldClient {

	public static void main(String[] args) {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress("http://localhost:8080/HelloWorld");
		factory.setServiceClass(HelloWorld.class);
		HelloWorld helloWorld = (HelloWorld) factory.create();
		System.out.println(helloWorld.sayHello("KingAragorn"));
	}
}

Hello KingAragorn




分享到:
评论
1 楼 liang1022 2012-10-22  
請問要怎麼在命列行 启动服务器呢?
java MainServer 都會報錯耶

相关推荐

Global site tag (gtag.js) - Google Analytics