Rest API Java + Postgresql
REST API DOCUMENTATION
Rest API
REST (REpresentational State Transfer) is an architectural style, and an approach to communications that is often used in the development of Web services. The REST style emphasizes that interactions between clients and services is enhanced by having a limited number of operations (verbs). Flexibility is provided by assigning resources (nouns) their own unique Universal Resource Identifiers (URIs). Because each verb has a specific meaning (GET, POST, PUT and DELETE), REST avoids ambiguity.
REST API IDE
- At first I choose Eclipse for developing RestApi sample application but there is lots of disadvantage are there while using eclipse.some sample things are below
Disadvantage:
- You have to install lots of application to run restapi.I install Eclipse Mars.2 Release (4.5.2), JDk 1.7,tomcat 8.5.6 and jersey Framework libraries.
- You have to add those jersey files externally inside the Eclipse IDE,then if you run the sample rest api program in eclipse it shows an lots of error.
- At first i try the below link but it won’t run it shows lots of error
2. Then I move into netbeans IDE.It have lots of advantage over Eclipse IDE
Advantage:
- You don't need to install jersy,tomcat because Netbeans 8.1 have default jersey libraries and it have default Glassfish instead of using tomcat.
- In netbeans you can directly move to coding part instead of wasting time in downloading and installing extra packages.
- In netbeans you can use either tomcat or glassfish server
Environment Setup for REST API in Ubuntu using NETBEANS 8.1
Step 1: Download and install Netbeans from following URL.
Give Executable permission : $ chmod +x ~/Downloads/netbeans-8.1-linux-*.sh
Start Installer : $ cd ~/Downloads && ./netbeans-8.1-linux-*.sh
Set JDK location : Browse to JDK location
Step 2: Create New Web Application ( Sample Application to Test Rest web service)
File=>New Project=>Java Web=>Web Application=>Next => Enter Project Name => next=>Select Server (Either GlassFish/Tomcat) => next=>Finish.
Step 3: Refer instruction
If you are a beginers then follow the link
Step 4:some sample codes:
Step 5: ERRORS I PERSONALLY FACED:
Error : The module has not been deployed. See the server log for details.
GlassFish Server 4.1.1, deploy, Connection refused, false
Solution : The solution is I stop my server and restart my server once again
To stop server :$ ./stopserv
To start it: $ ./startserv
Because in the error it says the port is occupied by some other server.so if you can restart your server you can solve the error.
ENVIRONMENTAL SETUP FOR POSTGRES DATABASE
POSTGRES DATABASE:
PostgreSQL (pronounced "post-gress-Q-L") is an open source relational database management system ( DBMS ).PostgreSQL supports transaction s, subselects, trigger s, view s, foreign key referential integrity, and sophisticated locking. It runs on numerous platforms including Linux , most flavors of UNIX , Mac OS X , Solaris , Tru64, and Windows . It supports text, images, sounds, and video, and includes programming interfaces for C / C++ , Java , Perl , Python , Ruby, Tcl and Open Database Connectivity ( ODBC ).
Setup and Requirements:
Step 1: Download and install Postgres DB from following URL.
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
sudo apt-get install postgresql postgresql-contrib
Create a user account called postgres which is associated to postgres role
sudo -i -u postgres
Postgre is prompt by immediately typing
psql
For Exit Postgres type
\q
For enter into Postgres use this keyword
sudo -u postgres psql postgres
For creating database password
\password postgres
Step 2: password setup:
If you want to ask password every time when you login to your database for security purpose.you can change the Configuration file.To do that follow the below step,
File location : $ sudo gedit /etc/postgresql/9.3/main/pg_hba.conf
Change the configuration:
Database administrative login by Unix domain socket
local all postgres md5
After change the configuration file you can restart your service
sudo service postgresql restart
To view postgres status
$ sudo service postgresql status
Step 3:Work on postgresdb:
Create database in Postgres
postgres=# create database databasename
For connecting database
postgres=# \c databasename
For disconnecting database
postgres=# \q
Create table tablename ();
postgres=# CREATE TABLE table_name( column1 datatype, column2datatype,
column3 datatype,.....column N data type,PRIMARY KEY( one or more columns ));
column3 datatype,.....column N data type,PRIMARY KEY( one or more columns ));
To view list of tables
postgres=# \d - is to view list of new tables that are created
postgres=# \dt - is to view list of tables that are present in database
To delete table
postgres=# Drop table tablename;
To insert a data in table:
postgres=# INSERT INTO TABLE_NAME (column1, column2, column3,...column N)VALUES (value1, value2, value3,...value N);
To view the datas in the table :
SELECT * FROM table_name;
Refer the link for syntex:
https://www.tutorialspoint.com/postgresql/index.htm
Access the postgresDB using java with Rest API in Netbeans 8.1
I done those things in two methods;
- Develop java code
- Without develop a java code(using Netbeans IDE)
Without develop a java code(using Netbeans IDE)
Step1:Create a database connection
Java DB database server is part of NetBeans. We will use Java DB as the database server. The following steps demonstrate creating the database playerDB.
1.
|
To start the Java DB Database from NetBeans, perform the following steps.
a. Click Services tab.
b. Expand Databases node.
c. Right-click Java DB icon.
d. Select Start Server.
that the DB server has started
|
2.
|
To create test1 database, perform the following steps:
a. Right-click Java DB icon, select Create Database.
b. Enter the following information for the database:
Database Name: test1
User Name: postgres
Password: admin
Confirm Password:admin
c. Click OK.
This creates the database and adds a connection for the database under the Databases icon.
|
3
|
To connect to the newly created database test1, perform the following steps :
a. Right-click jdbc:postgresql://localhost:5432/test1 [postgres on POSTGRES] connection.
b. Select Connect.
|
4.
|
Examine the contents of the database.
a. In the Services window, expand the jdbc:postgresql://localhost:5432/test1 [postgres on POSTGRES] connection under the Databases node.
b. Right-click the connection and select Refresh.
c. Expand the public schema. You see the nodes for Tables, Views, and Procedures.
d. Expand the Tables node to see the tables details.
e. Right-click the employee table node and select View Data.
|
Step 2:Building a Sample Web Application
To create RESTful Web Services, you need a Java Web application project. In the below section you will create a demo Java web project, test1.
1.
|
To create new Java Web Project, select File > New Project.
|
2.
|
Select Java Web from the Categories column and Web Application from the Projects column and click Next.
|
3.
|
Perform the following steps:
a. Name the project RestProject.
b. Click Next.
|
4.
|
In the Server and Settings window, verify GlassFish Server is selected as Server and click Finish.
|
5.
|
To start the Server, perform the following steps :
a. Open the Projects tab.
b. Right-click restproject1 project.
c. Select Run.
This action starts the GlassFish server and deploys the application.
On successful deployment of the application - a default jsp page with url, http://localhost:8080/restproject1/ is opened in the browser displaying "TO DO WRITE".
|
Step 3:Generate RESTful Web Services
The below section demonstrates how to use the RESTful Services from Database wizard to generate entity classes and RESTful Web Services in the same process.
1
|
To generate RESTful Web Services do the following:
Right-click the Restproject1 and choose New > Other > Web Services > RESTful Web Services from Database. The New RESTful Web Service wizard opens on the Database Tables panel.
|
2.
|
In the Database Tables window, select Data Source.
|
3.
|
Next select "New Data Source" from the Drop-down list.
a. In the Create Data Source Window, enter the following information:
b. Click OK.
The Employee tables are displayed under the Available Tables column.
c. Click Add All.
d. Click Next.
|
4.
|
In the Entity Classes window, complete the following steps:
a. Enter the package name as com.test1.
b. Click Next.
|
5.
|
In the Generated Classes Window, click Finish with default values.
|
6.
|
In the Rest Resources Configuration Window, click OK.
|
7.
|
In the Projects pane, perform the below steps :
a. Select and expand the restproject1 project.
b. Expand the source packages of the project.
c. Expand the com.test1 package.
d. Verify the creation of Entity Classes Employee.java
e. Expand the RESTful Web Services folder and and verify the creation of two RESTful Web Services
The above generated two Web Services are the REST front end of our application. For each entity, there is a resource that lists all the entity's instances.
|
Step 4:Test RESTful Web Services
The following section demonstrates how to test RESTful Web Services with tests that are generated using the test framework provided by Jersey.
1
|
To Generate Web Services Test client, complete the following steps.
a. Select Restproject1 project.
b. Right-click and select Test RESTful Web Services.
Configure REST test Client window opens.
|
2
|
Select "Web Test Client in project" and click Browse.
|
3
|
a. In the Select Project dialog box, select Restproject and click OK.
b. Configure Rest Test Client window is displayed, click OK.
The server starts and the application is deployed. When deployment is complete, the browser displays your application, with a link for each of the Web Services.
Out put:
|
Refer the link:
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices/RESTfulWebservices.htm
Develop java code:
Step 1: In NetBeans IDE, create a simple web application. Open NetBeans IDE.
Select File->New Project.
From Categories, select Java Web. From Projects, select Web Application. Click Next.
Enter a project name,
Make sure the Server is GlassFish v3
Click Finish.
Step 2: First we need to create a package called Restproject1. To do this, right click on src in package explorer section and follow the option: New -> Package.Next we will create Webservices.java,dto.java,dto.java,Model.java files under the Restproject1 package.
The source codes are present in the below Link: Refer code Google Drive URL
Step 3: Test RESTful Web Services:
- Test the web service. To do this, right-click the project node and click Test RESTful Web Services.
- This step will deploy the application and bring up a test client in the browser.
- Deploy and Run the application.
Step 4: The output:
Reference link:
https://codezone4.wordpress.com/2012/11/08/restful-web-services-java-mysql-and-json/
Comments
Post a Comment