服务端
@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!");
}
}
在cxf服务端的方法中的参数上注解为ws调用的参数
跨协议@WebParam(name = "sex") 这部分不能少
客户端访问
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" }));
}
}