CXF应用程序开发 WS 采用Simple Frontend 方式简单方便

 

关于CXF中的Simple Frontend  的官方网站如下:

http://cxf.apache.org/docs/simple-frontend.html

 

CXF includes a simple frontend which builds services from reflection. This is in contrast to the JAX-WS frontend which requires you to annotate your web service classes or create a WSDL first. The simple frontend will use reflection to intelligently map your classes to a WSDL model.

By default CXF uses the JAXB databinding. If you are interested in a databinding which does not require annotations, please see the documentation on the Aegis Databinding (2.0.x).

上面的意思如下:

  CXF的包括一个简单的前端建立在反射的基础上的服务。这是在对比了JAX - WS的前端,需要你来注释的web服务类或创建一个WSDL。简单的前端会使用反射类映射到上一个WSDL模型中。默认情况下使用了JAXB CXF的数据绑定。如果你是在一个数据绑定不感兴趣。

具体代码如下:

package com.easyway.cxf.service;

import java.util.List;

import com.easyway.cxf.model.User;
/**
 * 服务的类
 * @author longgangbai
 *
 */
public interface HelloService {
 /**
  * @param name
  * @return
  */
  public String hello(String name);
 
  /**
   * Advanced usecase of passing an Interface in.  JAX-WS/JAXB does not
   * support interfaces directly.  Special XmlAdapter classes need to
   * be written to handle them
   */
  public String sayHi(User user);

  public String[] getAllUseNames(List<User> userList);
}

package com.easyway.cxf.service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.easyway.cxf.model.User;
/**
 * 服务的实现类
 * @author longgangbai
 *
 */
public class HelloServiceImpl implements HelloService {

    Map<Integer, User> users = new LinkedHashMap<Integer, User>();
   

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

 public String sayHi(User user) {
         users.put(users.size() + 1, user);
         return "Hello "  + user.getUsername();
 }
 public String[] getAllUseNames(List<User> userList) {
  String[] userListArr=new String[userList.size()];
  for (int i=0;i<userList.size();i++) {
   userListArr[i]=userList.get(i).getUsername();
  }
  return userListArr;
 }

}

package com.easyway.cxf.test.client;

import org.apache.cxf.frontend.ClientProxyFactoryBean;

import com.easyway.cxf.service.HelloService;
import com.easyway.cxf.test.server.ApacheCFXServer;
/**
 *
 *
 * @author longgangbai
 *
 */
public class ApacheCFXClient {
 public static void main(String[] args) {
  ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
     factory.setServiceClass(HelloService.class);
     factory.setAddress(ApacheCFXServer.WS_ADDRESS);
     HelloService client = (HelloService) factory.create();
     System.out.println("Invoke sayHi()....");
     System.out.println(client.hello(System.getProperty("user.name")));
 
 }
}

package com.easyway.cxf.test.server;

import org.apache.cxf.frontend.ServerFactoryBean;

import com.easyway.cxf.service.HelloService;
import com.easyway.cxf.service.HelloServiceImpl;

public class ApacheCFXServer {
 public static final String WS_ADDRESS="http://localhost:8080/cxf/services/helloService";
public static void main(String[] args) {
 HelloServiceImpl helloWorldImpl = new HelloServiceImpl();
 // Create our Server
 ServerFactoryBean svrFactory = new ServerFactoryBean();
 svrFactory.setServiceClass(HelloService.class);
 svrFactory.setAddress(WS_ADDRESS);
 svrFactory.setServiceBean(helloWorldImpl);
 svrFactory.create();
}
}

 

你可能感兴趣的:(apache,Web,UseCase)