First you need to download EclipseLink and unpack the zip. Next step is to set the JAVA_HOME and provide the JDBC Drivers location. Go to the eclipselink\utils\dbws folder and edit the setenv.cmd or setenv.sh file.
set JAVA_HOME=xxxxx\jdk160_18
set DRIVER_CLASSPATH=xxxxx\wlserver_10.3\server\lib\ojdbc6.jar;C:\oracle\MiddlewareJdev11gR1PS2\wlserver_10.3\server\lib\aqapi.jar
You can replace xxxxx with any 10.3.2 or 10.3.3 Middleware home or use a JDeveloper 11g R1 home.
For this blogpost, I will use the emp and dept table of scott demo schema ( Part of the Oracle Database ) and use a PL/SQL package. Here is the code of the package, this packages contains two functions, the first returns the time in the required format and the second return all the departments in a ref cursor.
create or replace package scott_ws is
function get_current_time(date_format varchar2) return varchar2;
function get_all_department return SYS_REFCURSOR;
end scott_ws;
/
create or replace package body scott_ws is
function get_current_time(date_format varchar2) return varchar2 is
v_time varchar2(20);
begin
if date_format is null then
return 'empty format';
end if;
select to_char(sysdate, date_format) into v_time from dual;
return v_time;
exception
when others then
return 'error';
end;
function get_all_department
return SYS_REFCURSOR
is
dept_recordset SYS_REFCURSOR;
begin
OPEN dept_recordset FOR SELECT * FROM dept;
return dept_recordset;
end get_all_department;
end scott_ws;
/
Next step is to configure the dbws-builder xml file. ( Click here for more information about the dbws-builder xml ) The first properties part is necessary information for the DBWS generator. Off course you need to provide the JDBC Driver details. The dataSource property is for the JNDI name of the JDBC Datasource, this need to be created on the WebLogic Server.
<?xml version="1.0" encoding="UTF-8"?>
<dbws-builder xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<properties>
<property name="projectName">scott</property>
<property name="username">scott</property>
<property name="password">tiger</property>
<property name="url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="driver">oracle.jdbc.OracleDriver</property>
<property name="dataSource">jdbc/scottDS</property>
<property name="targetNamespace">http://www.whitehorses.nl/scott</property>
<property name="logLevel">finest</property>
<property name="contextRoot">/MyScottServices</property>
</properties>
<table catalogPattern="%" tableNamePattern="EMP">
<sql name="findEmpByDept2" isCollection="true" returnType="empType">
<text>
<![CDATA[select * from EMP where DEPTNO like ?]]>
</text>
<binding name="DEPTNO" type="xsd:int"/>
</sql>
</table>
<table catalogPattern="%" tableNamePattern="DEPT"/>
<sql name="findEmpByDept" simpleXMLFormatTag="employees" xmlTag="employee">
<text>
<![CDATA[select * from EMP where DEPTNO like ?]]>
</text>
<binding name="DEPTNO" type="xsd:int"/>
</sql>
<procedure catalogPattern="scott_ws"
schemaPattern="SCOTT"
procedurePattern="get_all_department"
isCollection="true"
simpleXMLFormatTag="departments"
xmlTag="department"/>
<procedure catalogPattern="scott_ws"
schemaPattern="SCOTT"
procedurePattern="get_current_time"
simpleXMLFormatTag="scott_ws"
xmlTag="time"/>
</dbws-builder>
To expose a table as Web Service you can use this
<table catalogPattern="%" tableNamePattern="DEPT"/>
You can also use a SQL statement with or without any bind variables. Here is it handy to provide the simpleXMLFormatTag and xmlTag attributes.
<sql name="findEmpByDept" simpleXMLFormatTag="employees" xmlTag="employee">
<text>
<![CDATA[select * from EMP where DEPTNO like ?]]>
</text>
<binding name="DEPTNO" type="xsd:int"/>
</sql>
To expose a function in a package I can use this and because this will return all the departments, the isCollection attribute need to be true and here is it also good, to provide the simpleXMLFormatTag and xmlTag attributes.
<procedure catalogPattern="scott_ws"
schemaPattern="SCOTT"
procedurePattern="get_all_department"
isCollection="true"
simpleXMLFormatTag="departments"
xmlTag="department"/>
The last part of this dbws-builder xml is the emp table with inside a SQL statement. This is handy when you want to re-use the emp complex type. In this case I will also use the empType ( table name + Type ) in the SQL statement ( returnType attribute ).
We are ready to generate some Web Services based on this dbws-builder xml.
Go to the eclipselink\utils\dbws folder and start this in a cmd box.
dbwsbuilder.cmd -builderFile scott_emp.xml -stageDir c:\temp\test -packageAs wls scott.war
this will generate a war which can be deployed on WebLogic. you can also use -packageAs jdev or -packageAs eclipse. Then you can include this code in your JDeveloper or Eclipse project.
Go to the Weblogic Console ( http://xxxx:7001/console ) and create the JDBC Datasource ( jdbc/scottDS ) and target this datasource to the right servers.
Go to deployments and click on install, Select your WAR.
After deployment you can click on the application and open the web service
Click on the Test Client to test your WSDL operations.
The last optional step is to configure the security on these web services. Go to the Configuration / WS-Policy Tab and select the endpoint.
You can choose for the WS security policies of OWSM or those of Weblogic. To configure OWSM for this Web Service you can use this blogpost.
That's all






First of all, thanks for an excellent blog.
ReplyDeleteAre you aware of this: http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10492/xdb_web_services.htm#ADXDB5676
It's basically a native way of exposing data and procedures without any code. And when coupled with OSB it really makes securely publishing legacy procedures as services a really simple trick.
Hi,
ReplyDeleteI know this XDB feature and I am using it for rest web services in combination with OSB.
+ve of my solution is database independed, no licensing ( don't need to have WLS or Ora DB) and you don't need to know OSB.
I think Oracle customer will use Soa Suite or OSB with the DB /EJB adapter.
thanks
Hi Edwin,
ReplyDeleteI tried out this and working perfectly...
Thanks...
Do you know if you can use DBWS as a REST service as well?
ReplyDeleteHi,
ReplyDeleteI dont thinks so , this is a DB to WS framework.
But you can check jersey or one of its implementation
https://jersey.dev.java.net/
thanks
Could you please specify which version of WebLogic did you use? 10gR3 (10.3.0) does not seem to work....
ReplyDeleteHi,
ReplyDeleteI think it is 10.3.3 (ps3) or 10.3.2. Do you get a particular error message.
you can try to download the latest eclipselink and update these jars in weblogic.
thanks
Yes, it fails with
ReplyDelete[EclipseLink-47000] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DBWSException
Exception Description: Could not locate file [eclipselink-dbws-sessions.xml]
No matter what I try. Though I have tried it with 10.3.0 and JBoss 5.1.0 only so far.
Hi,
ReplyDeletecan you check the generated war, it should be located in WEB-INF\classes\META-INF
and add the eclipselink jars to the setDomainenv.bat
thanks