Thursday, August 1, 2013

Java WebService Tutorial - Part 09 (Writing a Simple JAX-WS Style WebService Using NetBeans and GlassFish Server)

In the previous post we discussed, what are the types of web services and the different approaches. Apart from that we saw how can we write web services in different styles.In this post we will be seeing how to write JAX Style webservice using NetBeans and how to deploy and run in GlassFish Server.

What is JAX-WS ?

- Java API for XML Web Services (JAX-WS) is a technology for building web services and clients that communicate using XML.

- JAX-WS allows developers to write message-oriented as well as Remote Procedure Call-oriented (RPC-oriented) web services.

- In JAX-WS, a web service operation invocation is represented by an XML-based protocol, such as SOAP. The SOAP specification defines the envelope structure, encoding rules, and conventions for representing web service invocations and responses. These calls and responses are transmitted as SOAP messages (XML files) over HTTP.(More on SOAP can be found here: http://ayazroomy-java.blogspot.in/2013/07/java-webservice-tutorial-part-02.html

- Although SOAP messages are complex, the JAX-WS API hides this complexity from the application developer.

- With JAX-WS, the developer does not generate or parse SOAP messages. It is the JAX-WS runtime system that converts the API calls and responses to and from SOAP messages.

- With JAX-WS, clients and web services have a big advantage: the platform independence of the Java programming language.

- In addition, JAX-WS is not restrictive: A JAX-WS client can access a web service that is not running on the Java platform, and vice versa. This flexibility is possible because JAX-WS uses technologies defined by the W3C: HTTP, SOAP, and WSDL. WSDL specifies an XML format for describing a service as a set of endpoints operating on messages.

So now we got to know what is JAX-WS is now we require the following to develop our first webservice.

Requirements:

1. NetBeans ID
2. GlassFish Server

Download  NetBeans ID which usually comes along with GlassFish Server.

Step 1 :

Open NetBeans .

Select File--->New--->New project .

Select JavaWeb-->WebApplication . Give a Project Name say "MyJAX" and press the "Next"
Button. Select the Server as "GlassFishServer" and press the "Finish" Button.

Step 2:

Right Click on the Project Node[MyJAX] and select New-->Other-->WebServices-->WebService and press the "Next" button.

Give a name to the WebService say "showservice" and give package name as "com.test" and press "Finish" Button.

A showservice Java class will be created with some default methods.Remove all from the source code and put the following code:

Code:

package com.test;

import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;

 @WebService(serviceName = "showservice")
public class showservice {

public List<String> getBooks()
{
ArrayList<String> list=new ArrayList<String>();
list.add("JAVA");
list.add("C++");
list.add("PERL");
return list;
}

}

If you see this code , this is a simple Java class which is annotated by @WebService to make this class as Webservice, that's it you have successfully created a simple JAX style webservice.They are lot of other annotations available we will discuss later.Every thing is taken care by the server.Now Deploy this webservice.


Step 3:

Deploying  the webservice.

Before Deployment make sure you have set the server as "GlassFish Server" by right clicking on the Project Node and select "properties" and select "Run"Option and see the Server is GlassFish or not otherwise change the server to GlassFish and press "Ok".

- Now we are ready for Deployment.Again Right Click on the Project Node[MyJAX] and select "clean and Build" .

- Once Project is successfully build select Right  Click on the Project Node[MyJAX] and select "Deploy".

 - Once the Deployment is done, You can see a folder called "Web Services" generated under you Project here MyJAX.

- Click on the WebServices folder , you can see our service will be there namely "showservice".Right click on the service and select "Test WebService" , a window will be open in the browser with the follwing URL

http://localhost:8080/MyJAX/showservice?Tester

You can see the WSDL  link will be available in this window.Click on that link to view WSDL.

This URL may change depend upon your server port and project Name.













Click on the getBooks Button to view all the Books. You can also test this service using SOAP UI.You know how to do that already.If not you can refer here :

"http://ayazroomy-java.blogspot.in/2013/07/java-web-service-tutorial-with-soap-ui.html"

That's all folks , we have created a webservice using JAX-WS API , there are lot of other annotations and other stuff there still we will see that in the later series our tutorial.



<<Prev                                    Next >>                                Index                     

---------------------------------------------------------------------------------------------




Technology Blogs
blogs

4 comments:

  1. Thanks for sharing this article.
    Will you elaborate other annotations used in JAX-WS like WebMethod, WebResult, OneWay, HandlerChain, etc & there parameters like endpointInterface, serviceName, portType, targetNamespace, file, etc.
    It will be very helpful for us to write a powerful JAX-WS. I'll wait for your this post. Hope you will post soon.

    ReplyDelete
  2. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me. java training institute in chennai

    ReplyDelete
  3. Bermain Di Situs BandarQ Online di dalam situs memerlukan beberapa persyaratan. Salah syaratnya adalah cukup umur dan memiliki akun permainan. Untuk bisa mendapatka akun permainan
    asikqq
    dewaqq
    sumoqq
    interqq
    pionpoker
    bandar ceme terbaik
    hobiqq
    paito warna terlengkap
    syair sgp

    ReplyDelete