Java的web service核心代码如下:
package com.tw.webservice;
import javax.jws.WebService;
@WebService(targetNamespace = “http://webservice.tw.com/“, endpointInterface = “com.tw.webservice.HelloWorld”, portName = “HelloWorldImplPort”, serviceName = “HelloWorldImplService”)
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello() {
// TODO Auto-generated method stub
return “Hello World !”;
}
@Override
public String sayHi(String name) {
// TODO Auto-generated method stub
return “Hi ” + name;
}
}
WSDL地址:http://172.20.8.77:8080/ClassicService/services/HelloWorldImplPort?wsdl
在PL/SQL新建function如下:
create or replace function classicService1(name in varchar2) return varchar2 is
v_namespace VARCHAR2(1000) := 'http://webservice.tw.com';
v_service_qname utl_dbws.qname := utl_dbws.to_qname(v_namespace,'HelloWorldImplService');
v_port_qname utl_dbws.qname := utl_dbws.to_qname(v_namespace,'HelloWorldImplPort');
v_operation_qname utl_dbws.qname := utl_dbws.to_qname(v_namespace,'sayHi');
v_service utl_dbws.service;
v_call utl_dbws.call;
v_int_type utl_dbws.qname;
v_string_type utl_dbws.qname;
v_request_params utl_dbws.anydata_list; -- RPC style only
v_response_anydata AnyData; -- RPC style only
v_request_xmltype XmlType; -- Document style only
begin
v_service := utl_dbws.create_service(
HttpUriType('http://172.20.8.77:8080/ClassicService/services/HelloWorldImplPort?wsdl'), v_service_qname);
v_call := utl_dbws.create_call(v_service, NULL, v_operation_qname);
utl_dbws.set_property(v_call, 'SOAPACTION_USE', 'TRUE');
utl_dbws.set_property(v_call, 'SOAPACTION_URI', 'getName');
utl_dbws.set_property(v_call, 'ENCODINGSTYLE_URI',
'http://schemas.xmlsoap.org/soap/encoding/');
utl_dbws.set_property(v_call, 'OPERATION_STYLE', 'rpc');
v_int_type := utl_dbws.to_qname('http://www.w3.org/2001/XMLSchema', 'int');
v_string_type := utl_dbws.to_qname('http://www.w3.org/2001/XMLSchema', 'string');
utl_dbws.add_parameter(v_call, 'name', v_string_type, 'ParameterMode.IN');
utl_dbws.set_return_type(v_call, v_string_type);
v_request_params(0) := AnyData.ConvertVarchar2(name);
v_response_anydata := utl_dbws.invoke(v_call, v_request_params);
utl_dbws.release_call(v_call);
utl_dbws.release_service(v_service);
return(v_response_anydata.accessVarchar2);
EXCEPTION
WHEN OTHERS THEN
RETURN substr(sqlerrm,0,3000);
end classicService1;
经测试
1.service_qname、port_qname、operation_qname可以只定义成varchar2类型也能运行,但还是建议定义成utl_dbws.qname和底层保持一致。
2.utl_dbws.set_property必须要设置,否则报错:
ORA-29532: Java 调用被未捕获的 Java 异常错误终止: javax.xml.rpc.soap.SOAPFaultException: Message part name was not recognized. (Does it exist in service WSDL?)
3.这部分也是必须指定的,否则报 javax.xml.rpc.soap.SOAPFaultException或者参数异常
v_string_type := utl_dbws.to_qname(‘http://www.w3.org/2001/XMLSchema‘, ‘string’);
utl_dbws.add_parameter(v_call, ‘name’, v_string_type, ‘ParameterMode.IN’);
utl_dbws.set_return_type(v_call, v_string_type);
此外,还没想到方法调用无参数的函数,如这里的sayHello方法。