As you can see in my last 2 posts i have created a XML Schema, WSDL document and we have defined the operations that our webservice will perform,
Now its time to implement the business logic in the webservice.
Creating a Webservice from WSDL
- Right click the WSDL document that we have created and choose Generate Webservice. Follow the slides for basic webservice setups with No Policies defined.
- Press Finish and wait for Jdeveloper to finish his magical things.
- Now you can see Jdeveloper generated lot of Java classes and code in automatically, as you can see as well that myPortTypeImpl.java file create which provides the basic implementation of the webservice and Jdeveloper created all related Java classes on the types we have defined in the XML Schema so we can use them as a Java objects.
Retrieving Status from Oracle table via JDBC call
- Next it to retrieve the status of credit cards from database table for this we will use a simple JDBC statement to retrieve the values from the table but for his we have to add a JDBC library to our project
- To add a JDBC driver into your Project, Right click the project choose Project Properties go to Libraries and Classpath press Add Library and choose Oracle JDBC and press OK
- Now for code separation we will add the JDBC realated code to another Java Class, Right click the packages.types i.e baigsorcl.types package in application navigator and choose New, Select Java Class and enter name "OraJdbc" press OK
- Copy and paste the following code to the OraJdbc class (the purpose of this class is to create a connection via JDBC to oracle database and execute a query and return the status of credit card) and compile it (right click choose Make)
package baigsorcl.types; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class OraJdbc { public OraJdbc() { super(); } public String getCreditCardSatus(int ccNo) throws SQLException { //Return variable String result = "INVALID"; //Connecting to DB Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr"); conn.setAutoCommit(false); //executing query Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery ("SELECT creditcard_status FROM CreditStatus where Creditcard_number = " + ccNo ); //retreiving resultset while (rset.next ()) { result = rset.getString(1).toString(); } return result; } }
- Now call method of OraJdbc class in the myPortImpl class, Your getCreditCardStatus method should look like this
public CreditCardResponse getCreditCardStatus(CreditCardRequest part) { CreditCardResponse response = new CreditCardResponse(); OraJdbc callDb = new OraJdbc(); String result = "INVALID"; try { result = callDb.getCreditCardSatus(part.getCcNumber().intValue()); } catch (SQLException e) { System.out.println(e.getMessage()); } response.setCcStatus(result); return response; }
Thats all you are ready to Test your service.
Testing the Webservice
- Right click the myPortTypeImpl.java and choose Test Webserivce, an integrated WLS will start and opens a HTTP Analyzer tool which is available within JDeveloper to test the webservices.
- Enter the Credit Card number 1234 and press Submit Request, as you can see the result on the right side is appearing as VALID (because in our table this number has a as VALID status)
- Enter 9999 and you would see the result INVALID
Click the HTTP Context tab at the bottom to see the payload info in HTTP Format
Hope you have found the series useful, feel free to comments and ask questions i am always available to response your queries.
Download the complete workspace of Part 1,2 and 3
Happy JDeveloping,
Baig
i get invalid for everything
ReplyDeleteand some erros:
---
WSSERVLET11: failed to parse runtime descriptor: invalid value for attribute "implementation" of element "endpoint" in runtime descriptor (line 3)
com.sun.xml.ws.server.ServerRtException: invalid value for attribute "implementation" of element "endpoint" in runtime descriptor (line 3)
at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.failWithLocalName(DeploymentDescriptorParser.java:518)
[...]
at weblogic.t3.srvr.SubsystemRequest.run(SubsystemRequest.java:64)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
<08.07.2010 21:55 Uhr GMT+01:00> <User defined listener com.sun.xml.ws.transport.http.servlet.WSServletContextListener failed: com.sun.xml.ws.transport.http.servlet.WSServletException: WSSERVLET11: failed to parse runtime descriptor: invalid value for attribute "implementation" of element "endpoint" in runtime descriptor (line 3).
com.sun.xml.ws.transport.http.servlet.WSServletException: WSSERVLET11: failed to parse runtime descriptor: invalid value for attribute "implementation" of element "endpoint" in runtime descriptor (line 3)
[…]
at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:181)
Truncated. see log file for complete stacktrace
Caused By: com.sun.xml.ws.server.ServerRtException: invalid value for attribute "implementation" of element "endpoint" in runtime descriptor (line 3)
at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.failWithLocalName(DeploymentDescriptorParser.java:518)
at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.getMandatoryNonEmptyAttribute(DeploymentDescriptorParser.java:450)
at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parseAdapters(DeploymentDescriptorParser.java:221)
at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parse(DeploymentDescriptorParser.java:147)
at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized(WSServletContextListener.java:108)
Truncated. see log file for complete stacktrace
Download the sample workspace check if its working http://orclsamples.googlecode.com/files/WebServiceShow.zip
ReplyDeletehi,
ReplyDeletei am getting "two ports in the GetCustomers(webservicedescriptionbean)has the same name " while running the webservice implementation through jdeveloper 11g.
Please help me on this.
Hi,
DeleteSorry i don't understand what you mean.
ZB
Thank you for this tutorial. It's nice and simple. Can you write another tutorial, how with PL/SQL connect to WebService, get Resresponse and insert into table? Please.
ReplyDeleteHi,
DeleteIf you are talking in ADF context then you can expose any Java method and expose it as Service check this link for more info http://docs.oracle.com/cd/E21043_01/web.1111/b31974/web_services.htm
ZB