Here are a simple steps where you create a enterprise component using EJB3.0 and exposing the same as a webservice using JDeveloper.
Creating a simple enterprise java bean (JPA entity/application)
- Create new Application or a Project
- From the File menu, select New->General->Applications
- Enter the application name as AdharServiceApplication
- Select Generic Application in the Application template list and click Finish
- Create a new DB connection
- From the File menu, select New->General->Connections->Database connections
- Provide the connection details (connection name, connection type, schema user name and password, host details, service details ) and click Finish
- In the Application Navigator, you can expand the Connections and Database nodes in the Application Resources panel to see the database objects
- Create the Persistence objects ( JPA Entity)
- Launch the Create Entities from tables wizard (File->New->Business Tier->EJB->Entities from tables)
- step1 : select the EJB version 3.0
- step2 : no need to create a new persistence unit, simply click next
- step3 : select online database connections
- step4 : select the connection which you created above
- step5 : Query the POCADHAR table and shuttle it to the selected list
- click Finish
This
creates the Pocadhar class
- every entity object should have a primary key. Identify the instance variable and the accessor methods (getter & setter) and annotate it as @Id
- you will find this class annotated with @Entity
- No need of any changes in this XML.
- Create the Java service Session facade
- Select the persistence.xml and launch the create Java service Facade wizard (File->New->Business Tier->EJB->Java Service Facade)
- give the facade name as AdharJavaServiceFacade
- Tick the check box on 'Generate a main() method' and then click finish
- Open this newly created class AdharJavaServiceFacade and put the following code into the main method
final AdharJavaServiceFacade adharJavaServiceFacade = new AdharJavaServiceFacade();
Pocadhar newAdhar = new Pocadhar(); // create a new instance to persist newAdhar.setFirstname("Tupperware");
newAdhar.setLastname("Plastic");
newAdhar.setAddress1("DLF");
newAdhar.setCity("Hyderabad");
newAdhar.setCountry("India");
// You can create a dbtrigger to generate the values fo the primary key uniqueid, if not then you need to provide a value in the below call to persist a new row
//System.out.println("before call to persist ");
//System.out.println("before call to persist ");
adharJavaServiceFacade.persistPocadhar(newAdhar);
//System.out.println("after the call to persist ");
// the below lists all the rows from the POCADHAR database table
List<Pocadhar> pocAdharList = adharJavaServiceFacade.getPocadharFindAll();
for (Pocadhar adhar : pocAdharList) {
System.out.println("First name : " +adhar.getFirstname() + ". LastName : " + adhar.getLastname() + ". UniqueId : " + adhar.getUniqeid()+ ".");
}
- Run the AdharJavaServiceFacade.java from the Application Navigator. You should see the newly created adhar instance details in the database and as well as a list of all the rows displayed in the log window.
Creating the Webservice
Select
the AdharJavaServiceFacade.java from the application navigator window
and right click select Create Webservice, this launches the
webservice wizard
Message
Format :
tick
check box on SOAP 1.1 Binding
Message
format : Document / Wrapped
Methods
:Select the methods you want to expose as webservices
leave
the rest as defaults and click Finish.
Note
: you can view the wsdl generated by selecting 'Show WSDL for
webservice annotations' on right click of AdharJavaServiceFacade.java
You
can test this webservice either as an application or in a WLS
container
Deploy
the Webservice to a WLS
NOTE : There is a trivial problem while using the JAX-WS to marshall objects with no-arg default constructor.
Resolving java.sql.Timestamp does not have a no-arg default constructor
Problem : Usually JAXB expects a default constructor for the objects which it needs marshalling (object to XML).
You face problems when you use java.sql.Timestamp, java.sql.Date etc., object which does not have a no-arg constructor.
Solution : create the following adapter classes and info classes and build again. This should work.
1. Create a Timestamp adapter class
package project1.adapters;
import java.sql.Timestamp;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class TimestampAdapter extends XmlAdapter<Date, Timestamp> {
public Date marshal(Timestamp v) {
return new Date(v.getTime());
}
public Timestamp unmarshal(Date v) {
return new Timestamp(v.getTime());
}
}
public Date marshal(Timestamp v) {
return new Date(v.getTime());
}
public Timestamp unmarshal(Date v) {
return new Timestamp(v.getTime());
}
}
2. create a package-info.java at your project1 level.
@XmlJavaTypeAdapters(
@XmlJavaTypeAdapter(value=TimestampAdapter.class,type=Timestamp.class))
package project1;
@XmlJavaTypeAdapter(value=TimestampAdapter.class,type=Timestamp.class))
package project1;
import java.sql.Timestamp;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
import project1.adapters.TimestampAdapter;
Deploying the Webservice
deploying to the integrated server or to any application server through JDeveloper IDE
- Start the IntegratedWebLogicApplication server from the Application Server Navigator
- Select the top level application/project name in the Application Navigator and right click
- Select Deploy->Webservices..., this opens up the deployment wizard
- Deployment Action : Deploy to Application server
- Select Server : Select the application server (IntegratedWebLogicServer)
- Weblogic Options : leave the defaults and click Next
- Summary section : View and click the Finish
You
can test this webservice from
- Application server Navigator window
- The weblogic server console
- test the server as an application within the Jdeveloper IDE
- configure the wsdl generated through SOAPUI tool
You can edit the AdharJavaServiceFacade.java to add more functionality.
Say, you want to search for a ahdar details using the FIRSTNAME and get the related adhar records. Simply adding the below code to the AdharJavaServiceFacade.java and rebuild and re-deploy will make the service available.
public List<Pocadhar> findAdharByFirstNames(String firstName) {
final EntityManager em = getEntityManager();
return em.createQuery("select o from Pocadhar o WHERE o.firstname like :fname")
.setParameter("fname", firstName)
.getResultList();
}
The following steps explains you how to create a DB Sequence and attach the same to the uniqueID.
1. create a db sequence object with the name ADHAR_UNIQUE
CREATE SEQUENCE ADHAR_UNIQUE INCREMENT BY 1 MAXVALUE 9999999999999999999999999999 MINVALUE 10020 CACHE 20;
2. From the database navigator window, select the POCADHAR table and right click, select Trigger->Create(PK from Sequence)... and enter the details for Trigger Name, Sequenc Name as ADHAR_UNIQUE, Column name as UNIQEID and apply.
Hope, this is a very simple process using the JDeveloper in creating a EJB component and there after exposing it as a webservice.
Thanks,
Hari
Say, you want to search for a ahdar details using the FIRSTNAME and get the related adhar records. Simply adding the below code to the AdharJavaServiceFacade.java and rebuild and re-deploy will make the service available.
public List<Pocadhar> findAdharByFirstNames(String firstName) {
final EntityManager em = getEntityManager();
return em.createQuery("select o from Pocadhar o WHERE o.firstname like :fname")
.setParameter("fname", firstName)
.getResultList();
}
Database table used for this POC:
DESC POCADHAR
Name Null
Type
------------------------------ --------
UNIQEID
NUMBER
FIRSTNAME
VARCHAR2(50)
LASTNAME
VARCHAR2(50)
RELATIONNAME
VARCHAR2(50)
RELATIONTYPE
VARCHAR2(10)
ADDRESS1
VARCHAR2(50)
ADDRESS2
VARCHAR2(50)
WARD
VARCHAR2(50)
CITY
VARCHAR2(50)
STATE
VARCHAR2(50)
COUNTRY
VARCHAR2(50)
PINCODE
VARCHAR2(8)
The following steps explains you how to create a DB Sequence and attach the same to the uniqueID.
1. create a db sequence object with the name ADHAR_UNIQUE
CREATE SEQUENCE ADHAR_UNIQUE INCREMENT BY 1 MAXVALUE 9999999999999999999999999999 MINVALUE 10020 CACHE 20;
2. From the database navigator window, select the POCADHAR table and right click, select Trigger->Create(PK from Sequence)... and enter the details for Trigger Name, Sequenc Name as ADHAR_UNIQUE, Column name as UNIQEID and apply.
Hope, this is a very simple process using the JDeveloper in creating a EJB component and there after exposing it as a webservice.
Thanks,
Hari
No comments:
Post a Comment