1. axis2方式
a. 生成client端代码:
wsdl2java -uri F:\nantian\PSS\PSS_1.0_WSDL_Files\PSS_1.0_WSDL_Files\WSDLs\InventoryService.wsdl -p -g -t -s -o F:\temp\2016-4-15\axis2
这样就能生成Inventory的client和测试用例,打开InventoryServiceTest测试,这个地方要部分修改
InventoryServiceStub stub = new InventoryServiceStub();
//认证开始-----------
org.apache.axis2.client.ServiceClient client = null;
org.apache.axis2.client.Options option = null;
client = stub._getServiceClient();
option = client.getOptions();
List<org.apache.commons.httpclient.Header> list = new ArrayList<org.apache.commons.httpclient.Header>();
org.apache.commons.httpclient.Header header = new org.apache.commons.httpclient.Header();
header.setName("Authorization");
header.setValue("Bearer " + "t4wmagv86n8v8bt3y2k9kxvj");
list.add(header);
option.setProperty(
org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS,
list);
//认证结束------------
InventoryServiceStub.CustomerInventoryRequest customerInventoryRequest14 = (InventoryServiceStub.CustomerInventoryRequest)
getTestObject(InventoryServiceStub.CustomerInventoryRequest.class);
// TODO : Fill in the customerInventoryRequest14 here
//传参数开始-------------
CustomerIdsRequest ids = new CustomerIdsRequest();
ids.setCustomerIds(new String[] {});
customerInventoryRequest14.setCustomerInventoryRequest(ids);</span>
//传参数结束-------------
CustomersInventoryResponseE response = stub
.getCustomersInventoryIds(customerInventoryRequest14);
System.out.println(response);
2. CXF方式
a. 生成client端代码:
wsdl2java -p com.client -d F:\nantian\workspace\3.7.2\cxf\src -client
F:\nantian\PSS\PSS_1.0_WSDL_Files\PSS_1.0_WSDL_Files\WSDLs\InventoryService.wsdl
生成后如下:
修改以http_Client结尾的java文件:
InventoryServicePortType port = ss.getInventoryServiceSOAP11PortHttp();
//认证开始
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Authorization",
Arrays.asList("Bearer g3ssxwhud5385bzjt37us946"));
Client client = ClientProxy.getClient(port);
client.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);
//认证结束
{
System.out.println("Invoking getCustomersInventoryIds...");
com.cisco.inventoryservice.CustomerIdsRequest _getCustomersInventoryIds_parameters = null;
_getCustomersInventoryIds_parameters = new CustomerIdsRequest();
_getCustomersInventoryIds_parameters.getCustomerIds();//调用add增加相应参数
com.cisco.inventoryservice.CustomersInventoryResponse _getCustomersInventoryIds__return = port
.getCustomersInventoryIds(_getCustomersInventoryIds_parameters);
System.out.println("getCustomersInventoryIds.result="
+ _getCustomersInventoryIds__return);
}
add by 2017-10-31
可以不生成client,用JaxWsDynamicClientFactory动态生成客户端代理,只要知道wsdl的地址:
- public static void main(String[] args) {
- JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();
- bean.setServiceClass(HelloWorldService.class);
- bean.setAddress("http://localhost:9090/helloWorldService");
- HelloWorldService helloWorldService = (HelloWorldService)bean.create();
- String result = helloWorldService.sayHello("Kevin");
- System.out.println(result);
- }