Date:23/Jan/2023
----------------
Web Services
------------
- Web Services are open standard based web applications that interact
with other web applications for the purpose of exchaging data
- Web Services is a distributed technology acheives interoperability (portablity)
across different technologies like Java, .NET, PHP etc
Need of Web Services
--------------------
Refer diagram Need of Web Services.png
Types of Web Services
---------------------
- SOAP Web Services (self learning - will send the recording)
- REST Web Services
REST Web Services
-----------------
- REST stands for REpresentational State Transfer
- REST is web standard based architecture and uses HTTP protocol
for data communication
- REST uses various representations to represent the resource (web service)
like plain text, HTML, XML etc
JAX-RS Annotations (Java API for XML - REST Services)
------------------
@Path (your-path)
-----------------
sets the path to Base URL + your-path
Ex:
@Path("hello")
public class HelloResource
...
The client url/api to access HelloResource is
http://localhost:8080/RESTWSProj/hello
@GET
----
- uses HTTP GET method
- specifies method responds to GET request
Ex:
@Path("hello")
public class HelloResource
@GET
public String sayHello()
{...}
@Produces
---------
used to represent the resource in different representations
Ex:
@Produces(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_HTML)
@Produces(MediaType.TEXT_XML) or @Produces(MediaType.APPLICATION_XML)
Jersey Libraries
----------------
Jersey Libraries are used to develop REST Web Services
Developing a REST Web Service using Jersey Libraries and Maven
---------------------------------------------------------------
- Configure tomcat server in eclipse workspace
- Create a Maven Project
Click on File -> New -> Maven Project
Click Next
----------------------------------
Click Add Archetype
Archetype Group Id : org.glassfish.jersey.archetypes
Archetype Artifact Id : jersey-heroku-webapp
Archetype Version : 2.25
Click Ok
---------------------------------------
In Catalog Select : Internal/Default Local
In Artifact Id Select : jersey-heroku-webapp
Click NExt
Group Id : restws
Artifact Id : RESTWSProj
Package : com.ws.rest
Click Finish
Refer program MyResource.java (present in com.ws.rest package)
- Run the Project
Right click on RESTWSProj -> Run As -> Run on Server
- Open browser and type the following URLs/APIs
- http://localhost:8080/RESTWSProj/myresource/plain
- http://localhost:8080/RESTWSProj/myresource/html
- http://localhost:8080/RESTWSProj/myresource/xml
Date:24/Jan/2023
----------------
Create a REST Web Service which returns the product list from
the Product table
Producer
--------
- Create a table "Product" in MySQL
mysql>create table Product (pid int(3), pname varchar(10), price int(4));
mysql>insert into Product values (111,'monitor',5000);
...
mysql>insert into Product values (555,'speakers',1000);
- Add the following dependencies in pom.xml file of RESTWSProj
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
</dependency>
Refer program pom.xml
Note
----
Change the Version of Project to "JRE1.8" in Buildpath
- Create a POJO class "Product" in com.ws.rest package of src/main/java folder
of RESTWSProj
Refer program Product.java
- Create Hibernate XML configuration file "hibernate.cfg.xml" in
src/main/java folder
Refer program hibernate.cfg.xml
- Create a REST Web Service resource class "ProductService" in com.ws.rest package
Refer program ProductService.java
- Run the project (tomcat server, port no : 8080)
Right click on RESTWSProj -> Run As -> Run on Server
Consumer
--------
- Choose a new eclipse workspace "RESTClient"
- Configure tomcat server in eclipse
Note
----
Change the tomcat server port numbers
Tomcat Admin Port : 2222
HTTP : 3333
- Create a Maven Project
Click on File -> New -> Maven Project
Click Next
In Catalog Select : Internal/Default Local
In Artifact Id Select: jersey-heroku-webapp
Click Next
Group Id : restclient
Artifact Id : RESTWSProj
Package : com.ws.rest.client
click Finish
Note
----
Change the Version of Project to "JRE1.8" in Buildpath
- Create a POJO class "Product" in com.ws.rest.client package
Refer program Product.java
- Create a REST Web Service Client Servlet "ProductWebClient"
in com.ws.rest.client package
Refer program ProductWebClient.java
- Run the Servlet "ProductWebClient"
Right click on ProductWebClient.java -> Run As -> Run on Server
Refer programs in REST.zip