apache CXF实现了JAX-WS和JAX-RS Web服务范围,所以在CXF中,基于传统soap协议与restful风格的Web服务都支持。而且在CXF中Web服务的发布与访问 也有多种方式,这里就列举一下并做简要说明,下面是示例服务接口与服务实现类:
@WebService
@Produces("text/plain")
public interface HelloService {
@GET @Path("/{name}")
String sayHello(@PathParam("name")@WebParam(name="name")String name);
}
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello, " + name;
}
}
为了重用接口,就把soap协议服务与restful服务的相关注解标在同一个接口上,package均为com.nightsoul.cxf。
服务端:
public class ServerTest {
public static final String ADDRESS = "http://127.0.0.1:9000/hello";
/**
方式一、使用JDK中的API
使用这种方式时,JDK版本应该是不能低于JDK1.6.0_21,如果运行报错,应该就是JDK版本的问题
**/
@Test
public void testEndpoint() throws Exception {
//第一个参数为服务发布地址,第二个为服务实现类对象
Endpoint.publish(ADDRESS, new HelloServiceImpl());
}
/**
方式二、使用JaxWsServerFactoryBean
**/
@Test
public void testJaxWsServerFactoryBean() throws Exception {
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.setAddress(ADDRESS);
factoryBean.setServiceClass(HelloService.class);
factoryBean.setServiceBean(new HelloServiceImpl());
factoryBean.create();
}
/**
方式三、使用Simple FrontEnd
这种方式基本与方式二一样
**/
@Test
public void testSimpleFrontEnd() {
ServerFactoryBean factoryBean = new ServerFactoryBean();
factoryBean.setAddress(ADDRESS);
factoryBean.setServiceClass(HelloService.class);
factoryBean.setServiceBean(new HelloServiceImpl());
factoryBean.create();
}
/**
方式四、将Web服务配置在Spring中
当类路径下含有Spring时,CXF中的Bus会自动使用SpringBus实现类,从而会自动加载类路径下META-INF/cxf/cxf.xml与cxf.xml
文件,只要将服务Bean进行简单配置即可,JAXWS与JAXRS均支持
<jaxws:endpoint implementor="com.nightsoul.cxf.HelloServiceImpl" address="http://127.0.0.1:9000/hello"></jaxws:endpoint>
<jaxrs:server serviceClass="com.nightsoul.cxf.HelloServiceImpl" address="http://127.0.0.1:9000/hello"></jaxrs:server>
**/
@Test
public void testSpring() throws Exception {
SpringBusFactory factory = new SpringBusFactory();
factory.createBus();
}
/**
方式五、使用restful形式
@Produces注解用于表明服务返回的结果内容类型,@GET表示该方法只能通过get方式进行访问
@Path为该方法的路径,而完整的访问路径为JAXRSServerFactoryBean绑定的路径加上@Path配置的路径
例如:http://127.0.0.1:9000/hello/zhangsan,则name参数值为zhangsan
**/
@Test
public void testJaxRsServerFactoryBean() throws Exception {
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
factoryBean.setResourceClasses(HelloServiceImpl.class);
factoryBean.setBindingId(JAXRSBindingFactory.JAXRS_BINDING_ID);
factoryBean.setAddress(ADDRESS);
factoryBean.create();
}
@After
public void after() throws Exception {//防止服务端退出
Object object = new Object();
synchronized (object) {
object.wait();
}
}
}
客户端:
public class ClientTest {
/**
方式一、使用JDK中的API
**/
@Test
public void testEndpoint() throws Exception {
QName helloName = new QName("http://cxf.nightsoul.com/", "HelloServicePort");
Service service = Service.create(helloName);
service.addPort(helloName, SOAPBinding.SOAP11HTTP_BINDING, ServerTest.ADDRESS);
HelloService helloService = service.getPort(HelloService.class);
System.out.println(helloService.sayHello("xtayfjpk"));
}
/**
方式二、使用JaxWsProxyFactoryBean
**/
@Test
public void testJaxWsProxyFactoryBean() throws Exception {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setAddress(ServerTest.ADDRESS);
factoryBean.setServiceClass(HelloService.class);
HelloService helloService = factoryBean.create(HelloService.class);
System.out.println(helloService.sayHello("xtayfjpk"));
}
/**
方式三、使用JaxWsClientFactoryBean
**/
@Test
public void testJaxWsClientFactoryBean() throws Exception {
JaxWsClientFactoryBean bean = new JaxWsClientFactoryBean();
bean.setAddress(ServerTest.ADDRESS);
bean.setServiceClass(HelloService.class);
bean.create().invoke("sayHello", "xtayfjpk");
}
/**
方式四、使用Simple FrontEnd
**/
@Test
public void testSimpleFrontEnd() {
ClientProxyFactoryBean factoryBean = new ClientProxyFactoryBean();
HelloService helloService = factoryBean.create(HelloService.class);
System.out.println(helloService.sayHello("xtayfjpk"));
}
/**
方式五、将wsdl文件生成java类
使用cxf提供的wsdl2java工具会生成客户端调用类,将其加入类路径下,这种方式只对soap服务有效
**/
@Test
public void testWsdl2Java() {
HelloServiceService service = new HelloServiceService();
System.out.println(service.getPort(HelloService.class).sayHello("xtayfjpk"));
}
}
对于restful风格的Web服务,其访问方式与普通URL无差别,直接在浏览器中访问地址即可,如果愿意的话也可以使用HttpClient。