Web Services Java

Posted on  by

As you can see in the figure, Java.net, and PHP applications can communicate with other applications through web service over the network. For example, the Java application can interact with Java.Net, and PHP applications. So web service is a language independent way. Java Web Services. Java provides it’s own API to create both SOAP as well as REST web services. JAX-WS: JAX-WS stands for Java API for XML Web Services. JAX-WS is XML based Java API to build web services server and client application. JAX-RS: Java API for RESTful Web Services (JAX-RS) is the Java API for creating REST web services.

  1. Web Services Java
  2. Web Services Java Interview Questions
  3. Java Soap Web Service Tutorial
  4. Java Web Services Tutorial Point

In my previous article on web services I mentioned how www.idrsolutions.com received a lot of questions about Java development. In that article I gave a brief overview of what a web service is, how they work and how to communicate with one using a simple client.

In part two of a series of articles I will show you how to create your own simple java web service.

This article will also be useful for setting up BuildVu (Our PDF to HTML5 converter) with our own BuildVu Web Service.

First of all, we need to set up a server to host the web service. I’m using Glassfish 4.0, which allows you to host a local server on your own machine. You can install it yourself and point your IDE in the direction of it’s installation folder. Alternatively, with NetBeans you can download and install Glassfish (amongst others) when you create the project.

Now we need to create the project, in NetBeans:

  1. Go to File>New Project.
  2. Select the Java Web category and in the adjacent window select Web Application.
  3. Name your project and then press next.
  4. Select which server you want to use e.g. GlassFish. If no servers are available click Add and select Glassfish, press next and download the server.
  5. Press finish.
  6. You may get a page created called index.html, you can close it as it’s not needed.

in your projects window, right click the Source Packages folder, go to New and from the list Select Java Class. You can choose Web Service and it will create a fully generated web service class with some example methods. But for the time being, just create a new Java Class.

Now create a public method inside the new public method with the following code:

So your class should now look something like this:

The layout should be familiar to you as it’s exactly like any other Java class. So how do we turn it into a web service? Add the annotation @WebService above the class, add @WebMethod above the sayHello method and import their classes.

And now you’ve declared your java web service. It’s that simple!

Now that we’ve created the web service, we should see if it actually works. So to get the web service running on the server, follow these steps:

  1. Right click your project and select “Clean and Build”.
  2. After the projects been built, right click it again and select “Deploy”

The IDE will now launch an instance of Glassfish and put your web service up on it. Now we need to check that Glassfish has successfully deployed the service. To do this, we need to go to the console, which we access via a browser. Navigate to localhost:4848. Once the console is loaded, go to Applications on the sidebar.

You should be able to see your java web service listed as an application.

Click on the name of the web service to open the Edit Application. On the table at the bottom find the action called “View Endpoint”.

From there, we go to a page with a couple of links. The first link takes you to the previous page. The second link offers a tester utility which allows us to test the web service without the need of creating a client application, it also allows us to view the raw SOAP XML. The third one allows us to look at the actual WSDL generated for the web service.

Go to the second link (the url ending in ?Tester) and click the first link (with the port 8080). You should see the following:

Type something into the text box and press the “sayHello” button. We can now view the SOAP request and response generated.

You may have noticed that the elements in the SOAP body are generic with “sayHello” and “arg0” tags. to make these look nicer, we need to add some parameters to our annotations in the java web service class.

Redeploy the application and navigate back to the Tester utility, type in a vlaue and press “sayHello”, the SOAP XML should now look like this:

Web Services Java

In this article I’ve covered how to create a small web service and how to do some simple testing from within Glassfish. I would like to note that putting business logic inside of your Web Service isn’t a very good practice. Instead have a class which handles interaction from the web service interface and your business classes.

I also encourage you to play around with this web service and add methods of your own. Try looking at what happens when you use Lists as parameters and return values. Also try implementing your own client, using my previous tutorial if you need any guidance.

Got any questions or comments? Feel free to post them below.

Web services java tutorial

This post is part of our “GlassFish Articles Index” series. In these articles, we aim to explore GlassFish in different ways, from useful tutorials, to JaveOne and general.


Web Services tutorial is designed for beginners and professionals providing basic and advanced concepts of web services such as protocols, SOAP, RESTful, java web service implementation, JAX-WS and JAX-RS tutorials and examples.

Web service is a technology to communicate one programming language with another. For example, java programming language can interact with PHP and .Net by using web services. In other words, web service provides a way to achieve interoperability.

Java Web Services Tutorial

In this tutorial, you will be able to learn java web services and its specifications such as JAX-WS and JAX-RS.

There are two ways to write the code for JAX-WS by RPC style and Document style. Like JAX-WS, JAX-RS can be written by Jersey and RESTeasy. We will learn all these technologies later.

Web Services Examples

In this tutorial, we will see a lot of web services examples like JAX-WS example by RPC style, JAX-WS example by document style, JAX-RS example by Jersey and JAX-RS example by RESTeasy implementation.

Web Services Index

Web Services Tutorial

Java Web Services

JAX-WS Tutorial

Web Services Java Interview Questions

JAX-RS Tutorial

Interview Questions

Problem

Web services tutorial is developed and tested carefully. But if you find any problem or mistake in our tutorial, you can report to us. We assure, you will not find any problem in web services tutorial.


Java Soap Web Service Tutorial

next →

Java Web Services Tutorial Point