Introduction to Java Web Service Example
Nowadays java web services are used everywhere. When we need to interact with any web page, the web page response and request body. At the point when you associate with the site page, the program sends a solicitation and afterwards delivers a reaction and shows in the type of HTML. Essentially, web benefits likewise include solicitation and reaction yet as XML or JSON, or plain message. It is for the most part utilized for different applications or projects to burn through and utilizes data. Web service can be isolated generally into two gatherings, SOAP-based and REST-style. The differentiation isn’t sharp on the grounds that, as a code model later represents, a SOAP-based help conveyed over HTTP is an extraordinary instance of a REST-style administration.
Java Web Service Examples
But before examples, we need to understand what java web service is.
Basically, a Web service has two types that are SOAP and REST. The difference between them is not the same, there are some basic differences. The HTTP protocol is used for the SOAP web services, normally SOAP services use the simple protocol to deliver the different services or we can say that as per user requirement we can use any service. The SOAP services play a vital role in software design development, the SOAP web services only use in XML language. SOAP library is used for the service request and as well as it is also used for underlying the SOAP message. The customer and the web services source code might give not many clues, assuming any, about the fundamental SOAP as shown in the following figure as follows.
In REST web service there are no principles, toolkits or libraries. The main advantage of REST web service is that it avoids the complexity of SOAP web service.
Now let’s see the different features of java web services as follows.
1. Open foundation
Web services are conveyed utilizing industry-standard, merchant-free conventions like HTTP and XML, which are pervasive and surely known. Web services can piggyback on systems administration, information arranging, security, and different frameworks currently set up, which brings down section costs and advances interoperability among administrations.
2. Language straightforwardness
Web services and their customers can interoperate regardless of whether written in various programming dialects. We can use different programming languages such as C, C#, Java and Ruby, etc.
3. XML Based web service
A Java web service uses the XML data description and data transportation layer. With the help of XML language, we can make the connection with any network layer and operating system, etc.
4. Loosely Coupled
A customer of a web administration isn’t fixed to the web administration straightforwardly. The web administration interface can uphold development over the long run without arranging the customer’s capacity to speak with help. A firmly coupled framework implies that the customer and worker rationale are intently attached to each other, demonstrating that assuming one interface changes, another should be refreshed. Tolerating an approximately coupled design will in general make programming frameworks more reasonable and permits more clear incorporation between different frameworks.
5. Coarse supported
Java uncovers its capacities through individual strategies. A particular cycle is too fine an activity to give any reasonable ability whatsoever corporate level. Building a Java program without any preparation requires the production of different fine-grained capacities that are then gathered into a coarse-grained job that is devoured by either a customer or assistance.
Organizations and the interfaces that they demonstrate ought to be coarse-grained. Web administrations innovation executes a characteristic strategy for characterizing coarse-grained administrations that approach the perfect measure of business rationale.
6. Support Synchronous and Asynchronous
Synchronicity determines the limiting of the customer to the execution of the capacity. In the simultaneous summons, the customer squares and deferrals in finishing its administration prior to proceeding. Offbeat activities award a customer to summon an assignment and afterwards execute different capacities.
Non-concurrent customers get their outcome at a later point on schedule, while coordinated customers accept their impact when the help has finished. Non Concurrent capacity is a fundamental technique in empowering approximately coupled frameworks.
7. Modular design
Web services are intended to be measured in the plan so that new services can be produced through the coordination and layering of existing services. Envision, for instance, a stock following help incorporated with an internet requesting services to yield assistance that consequently arranges the suitable items in light of stock levels.
Now let’s see the different examples of java web services for better understanding as follows.
Now create a simple application by using JAX-WS, the jersey is the reference execution of JAX-RS API, it’s no piece of standard JDK and you need to incorporate every one of the necessary containers. The most ideal way is to utilize Maven construct, so make a basic Dynamic web task and afterwards convert it to Maven in Eclipse.
In the first step we need to add the dependencies into a pom.xml file whatever we require, just copy and paste the following code into the pom.xml file as follows.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SimpleWebServices</groupId>
<artifactId>WebServices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
After that, we need to create the .xml file and write the following code as follows.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"
version="3.1">
<display-name>JAX-RS-WelcomeInWebServices</display-name>
<servlet>
<servlet-name>Jersey REST Web Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.sample.jser.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Now in the third step, we need to create the java class file and write the following code as follows.
package demo;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@Path("/sample")
public class sample_demo {
@GET
@Path("/w_mssg/{w_msg}")
public String wel_msg(@PathParam(value="w_msg") String msg){
return "HI "+msg;
}
}
Explanation
The end result of the above program we illustrated by using the following screenshot as follows.
Conclusion
We hope from this article you learn more about the java web service example. From the above article, we have taken in the essential idea of the java web service example and we also see the representation and example of java web service example. From this article, we learned how and when we use the java web service example.
Recommended Articles
This is a guide to Java Web Service Example. Here we discuss the definition, examples with code implementation. You may also have a look at the following articles to learn more –