1、创建web Service Project
2、服务端主方法:
package com.hyan.service;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class ServiceHello {
/**
* 供客户端调用
*/
public String getvlaue(String name){
return "TheService-我叫:" + name;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Endpoint.publish("http://localhost:9001/Service/ServiceHello", new ServiceHello());
System.out.println("service success!");
}
}
3、启动服务端,将代码中地址输入浏览器访问得到wsdl地址: http://localhost:9001/Service/ServiceHello?wsdl;
4、客户端主方法:
package com.test;
import java.net.URL;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
try {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
String wsUrl = "http://localhost:9001/Service/ServiceHello?wsdl";
URL url = new URL(wsUrl);
org.apache.cxf.endpoint.Client client = dcf.createClient(url);
String method = "getvlaue";
Object[] result = client.invoke(method, "zhengmo");
System.out.println(result[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
5、运行客户端主方法:
九月 13, 2016 4:50:49 下午 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames
信息: Created classes: com.hyan.service.Getvlaue, com.hyan.service.GetvlaueResponse, com.hyan.service.ObjectFactory
警告: [options] 未与 -source 1.5 一起设置引导类路径
1 个警告
TheService-我叫:zhengmo
成功!