domenica 1 gennaio 2012

Create Web Services with netbeans7.0 and Glassfish 3.1

In this example I use:

- netbeans 7.0: http://netbeans.org/downloads/index.html
- glassfish3.1: http://netbeans.org/downloads/index.html
- postgresSQL 8.3: http://www.enterprisedb.com/products-services-training/pgdownload#windows
- pgAdminIII: http://www.enterprisedb.com/products-services-training/pgdownload#windows

Let's consider a very simple real example of a library where we define a basic model to describe the reality. There are three entities:

- author
- book
- reader

with these relations:

- an author write many books
- a book has only one author
- a book is read by many readers
- a reader read many books

so we can describe these relation as:

- author-book: one-to-many
- book-reader: many-to-many

With these rules we can  write our database:

create table author
(
id bigint not null,
cod_author varchar(10) not null,
des_author varchar(100) not null
);

create table book
(
id bigint not null,
cod_book varchar(10) not null,
des_title varchar(100) not null,
id_author bigint not null
);

create table reader
(
id bigint not null,
cod_reader varchar(10) not null,
des_reader varchar(100) not null
);

create table reader_book_ref
(
id_reader bigint not null,
id_book bigint not null
);


alter table author add primary key(id);
alter table book add primary key(id);
alter table reader add primary key(id);
alter table reader_book_ref add primary key(id_reader, id_book);

create unique index idx_author_cod_author on author(cod_author);
create unique index idx_book_cod_book on book(cod_book);
create unique index idx_reader_cod_reader on reader(cod_reader);

alter table book add foreign key(id_author) references author(id);
alter table reader_book_ref add foreign key(id_reader) references reader(id);
alter table reader_book_ref add foreign key(id_book) references book(id);

Now follow these steps:

First Step: open pgAdminIII and connect PostgresSQL server. Then right click on Database voice of the menu and create new database called libdb. Right click on db libdb and click on "CREATE code". Put script listed above on the window and execute to build our database libdb.

Second Step: create an empty folder BiblioProject, the open netbeans IDE and click file --> New project --> JavaEE --> Enterprise Application and fill the field of the wizard as it follows:


click next and confirm the build of ejb module and Web App Module as follows:


and click finish.

Click on the tab services, open Database-->Divers, if you don't have Postgres Driver right click on Drivers and add new Driver as follows:


Now add connection to libdb: right click on databases and add new Connection typing information as follows: 



Test connection to verify that is all correct.

Now turn back on the tab Projects, click on EABlio-ejb module--> new --> entity classes from database --> select data source --> add new data source:

add all tables in the available tables window on selected tables windows:



and click next. in the following window type the package where insert the classes generated com.entities:


Click finish and you can see that Netbeans generate for you all entity classes of your database. In this  case in com.entities we have insert the classes Author.java, Book.java, Reader.java that include jpa annotation.

Now right click on EABiblio-ejb module and click on "New Session Bean from Entity" and add all entity classes from Available Entity classes list to Selected Entity classes list:



click next, then replace package name with com.session and check local:


When you click on finish button Netbeans generate for you alla facade netbeans that will be the interface to your database.

At the end you can create a web service that allows you insert, update or extract data from database libdb. Right click on EABliblio-ejb module and click on "New Web Service" --> type WSAuthor as name of the web service, ws.author as package and check option "Create Web Service from Existing session bean" and browse for select the Session Bean AuthorFacade:



Follow the same step to create WSBook and WSReader. Now you have created Web Service!

To test the execution of the web service right click on EABiblio Application and click on deploy: glssfish server will be started and the application will be deployed on it. At this point you can go to tab Services, open voice servers on the tree and see that glasfish is started and it contains EABiblio applications  and WSBook, WSAuthor and WSReader services.

Turn back to tab projects and open voice Web Service on the tree of the EABiblio-ejb module, right click on WSAuthor and will be opened the browser with address http://localhost:8080/WSAuthor/WSAuthor?Tester, if yuo replace WSDL instead of Tester at the end of the address, you can see the wsdl generated. If you put Tester again:




 you can click for example on findAll() button and see the SOAP Response:







Nessun commento:

Posta un commento