标签:webservice cxf java
Apache CXF是一个开源的Service框架,可以用于简化用户的service开发,基于CXF开发的应用可提供SOAP、XML/HTTP、RESTFUL HTTP或CORBA等服务。CXF底层页可以使用不同的传输协议,包括HTTP、JMS或JBI等。
根据CXF官方的说明,CXF包含以下特性:
package test; import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHello(String name); }(4)在test包中创建HelloWorld接口的实现类HelloWorldImpl,代码为:
package test; import javax.jws.WebService; @WebService public class HelloWorldImpl implements HelloWorld { @Override public String sayHello(String name) { System.out.println("sayHello方法被调用"); return ("Hello"+name); } }(5)创建主类MainServer,用来发布webservice
package test; import javax.xml.ws.Endpoint; import org.apache.cxf.endpoint.Server; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class MainServer { <span style="white-space:pre"> </span>public static void main(String[] args) <span style="white-space:pre"> </span>{ //<span style="white-space:pre"> </span>//第一种发布方式:通过CXF提供的JaxWsServerFactoryBean来发布webservice //<span style="white-space:pre"> </span>JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); //<span style="white-space:pre"> </span>factory.setServiceClass(HelloWorldImpl.class); //<span style="white-space:pre"> </span>factory.setAddress("http://localhost:8080/HelloWorld"); //<span style="white-space:pre"> </span> //<span style="white-space:pre"> </span>Server server = factory.create(); //<span style="white-space:pre"> </span>server.start(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>//第二种方式,通过JAX-WS提供的Endpoint来发布webservice <span style="white-space:pre"> </span>//首先创建webservice服务提供类的实例 <span style="white-space:pre"> </span>HelloWorldImpl implementor = new HelloWorldImpl(); <span style="white-space:pre"> </span>String address = "http://localhost:8080/HelloWorld"; <span style="white-space:pre"> </span>Endpoint.publish(address, implementor); <span style="white-space:pre"> </span>} }执行服务端代码,可以看到eclipse输出如下,说明webservice成功发布
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("zhuwei")); } }就可以通过客户端访问webservice服务了。
标签:webservice cxf java
原文地址:http://blog.csdn.net/longshengguoji/article/details/41450177