使用CXF作为WEBSERVICE的服务端,用AXIS进行调用

当使用CXF作为WEBSERVICE的服务端,用AXIS进行调用的时候,发现与正常的WSDL文件缺少参数的定义和返回类型定义;需要在服务端的类中进行设置;如下:

@WebService
public interface HelloWorld {
 @WebResult(name = "String")
 String sayHello(@WebParam(name = "name") String name,
   @WebParam(name = "sex") String sex);

 void test();
}

 

@WebService
public class HelloWorldImpl implements HelloWorld {

 public String sayHello(String name, String sex) {
  if ("F".equals(sex)) {
   return "Hello," + name + "小姐";
  }
  if ("M".equals(sex)) {
   return "Hello," + name + "先生";
  } else {
   return "Hello," + name;
  }
 }

 public void test() {
  System.out.println("only test for privilege!");
 }
}

 

将类编写好后,只需要在服务端的配置中定义;如下:

 


 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
 
 
 
 
   address="/HelloWorld">
  
   
   
   

  
   
  

  
   
  

  
   
  

 

 
 
 
 
 

 

这样服务端的所有设置就都完成了,在客户端进行调用时,采用AXIS的方式,如下:

 

public class TestHelloWorld {
 public static void main(String[] args) throws Exception {
  Service service = new Service();
  Call call = (Call) service.createCall();
  call.setTargetEndpointAddress("http://localhost:8080/CFXDemoServer/services/HelloWorld");
  call.setOperationName("sayHello");
  call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);
  call.addParameter("sex", XMLType.XSD_STRING, ParameterMode.IN);
  call.setReturnType(XMLType.XSD_STRING);
  System.out.println(call.invoke(new Object[] { "harold!","l" }));

 }
}

 

通过这种方式,客户端不需要关心服务端采用了哪种实现方式!

 

你可能感兴趣的:(WebService,Bean,Apache,Spring,SOAP)