1.CXF简介
Apache CXF是一个开源的Service框架,可以用于简化用户的service开发,基于CXF开发的应用可提供SOAP、XML/HTTP、RESTFUL HTTP或CORBA等服务。CXF底层页可以使用不同的传输协议,包括HTTP、JMS或JBI等。
根据CXF官方的说明,CXF包含以下特性:
- 支持大量的Web Service标准,包括SOAP、WS-I Basic Profile、WSDL、WS-Addressing、WS-Policy、WS-ReliableMessaging和WS-Security。
- CXF支持大量的前端(frontend)编程模型。CXF实现了标准的JAX-WS API,它也包括一种被称为简单前端(simple frontend)的模型,这种模型无需annotation支持。CXF支持web service的两种开发模式:①规则(contract)优先的开发模式,即通过编写WSDL来开发web service;②代码优先的开发模式,即通过编写java代码来开发webservice.
2.CXF下载和安装
3.Web Service注释介绍
4.使用CXF开发web service
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("sayHello方法被调用");
return ("Hello"+name);
}
}
package test;
import javax.xml.ws.Endpoint;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class MainServer {
public static void main(String[] args) {
// 第一种发布方式:通过CXF提供的JaxWsServerFactoryBean来发布webservice
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloWorldImpl.class);
factory.setAddress("http://localhost:8080/HelloWorld");
Server server = factory.create();
server.start();
// 第二种方式,通过JAX-WS提供的Endpoint来发布webservice
// 首先创建webservice服务提供类的实例
/*
* HelloWorldImpl implementor = new HelloWorldImpl(); String address =
* "http://localhost:8080/HelloWorld"; Endpoint.publish(address,
* implementor);
*/
}
}
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:8090/HelloWorld");
factory.setServiceClass(HelloWorld.class);
HelloWorld helloWorld = (HelloWorld) factory.create();
System.out.println(helloWorld.sayHello("zhuwei"));
}
}
7424



被折叠的 条评论
为什么被折叠?



