Some Basics:
1. Web Services contained in Application Server provides services through XML/JSON using HTTP protocol.
2. JAX-WS is JAVA API for standard web services which simplifies developing web services using Java Technology.
3. Here we use WSDL XML (Web service Descripto Language) based web service. And parse WSDL to generate Web clients.
No more theoretical discussion here. There are huge theoretical discussions on Web.
Now we are concentrating on CRASHING hands on. Our steps are as follows:
1. Our tools are: NetBeans 7, GlassFish Server 3.1.2, JAVA 6. [i.e. you can use later versions]
2. Create a JAVA Web Application. [i.e. you can use your existing web Application]
3. Create Web Service.
4. Adding Operations to the Web Service.
5. Deploying and Testing the Web Service.
6. Create Client for Web Service.
7. Use the Client to call web service.
Download NetBeans from https://netbeans.org/downloads/
[NetBeans have aplication Server Tomcat & Glassfish.]
Check Servers from services tab in NetBeans. Here GlassFish & Tomcat are default servers. And I
manually added JBOSS for a different projects.
My Project is based on Glassfish. If you use GlassFish, then do the following otherwise ignore. In Tomcat you can call by client BUT can't test using Net Beans utility.
The above modification you can do easily. You can see the path from the properties of GlassFish under Services tab.
Go to the highlighted path and open domain1/config/domail.xml. Add the above line in appropriate place seen in figure.
Restart the server if it is already run.
1. Click File - New Project (Ctrl + Shift + N) or click New Project on Tool Bar.
OR
2. Select Java Web under Category and Web Application under Projects and click Next.
3. Give Project Name to MyWebApp and keep the all other settings as default and click Next.
4. Select GlassFish Server and Java EE 6 Web and click Finish.
5. Following files will be created in Project Explorer. Index.jsp file contained Hello World content to check the web application.
6. Now check the web application by Run.
1. Right click MyWebApp node and do the following.
2. Give name as MyWebService and package name as com.weservice as the following and click Finish. Don't use stateless session bean i.e. that is a huge topic which i'll cover in another few posts.
3. Now MyWebService.java under package com.webservice is added under Source Packages Node. And a new Web Services Node is added. Under this node, MyWebService node is added followed by hello:String node. hello is a default method created by Net Beans which return type String.
1. To add a new operation, open MyWEbService.java and go to Design view. And click Add Operation.
Or right click MyWebService under Web Services node and choose Add Operation..
2. Here I create an operation Addition which return type is Integer and it has 3 parameters of type int. So I've done the following and click OK.
Now if you expand the MyWebService node under Web Services node, then you'll see the operations list in your web service MyWebService.
Or if you go Design view of MyWebService.java under Source Packages, then you'll see the pictorial view of all your methods/operations.
3. Now go to Source view of MyWebService.java under Source Packages, I've only modified the return statements of my newly created Addition operation/method. i.e.
@WebService annotation, the service name specified explicitly. In later version or in JBOSS or GlassFish server, the annotation can be like
@WebMethod annotation specifies the the operation name in WSDL for clints.
@WebParam annotation defines mapping between parameters of operations of Web Services and WSDL entry.
1. Right click the project and choose Deploy. The IDE starts the application server, build the application and deploys it to the server. You can see the specific server log in output window.
2. Right click on MyWebService under Web Service node and choose Test Web Service
Give 1 2 3 in respective 3 text boxes and click Addition button, you'll see the following output. Or it can show exception if you give wrong input.
Now click Back button of browser. Click on WSDL File link. You'll show the following
Or you may go to http://localhost:8084/MyWebApp/MyWebService to see the web service details.
So your WSDL link is:
http://localhost:8084/MyWebApp/MyWebService?wsdl
For using your web service from remote host, replace localhost with IP Address.
http://10.80.190.60:8084/MyWebApp/MyWebService?wsdl
Now i'll use this WSDL link to create a web service client.
1. Go to cmd and issue command
wsimport -keep -p com.mywebservice.client http://10.80.190.60:8084/MyWebApp/MyWebService?wsdl
The java and class files will be located in com/mywebservice/client under the current directory. [i.e. C:\Users\khan.ashik]
2. 8 Java and corresponding 8 class files are created in that location.
3. 2 important Files are: MyWebService_Service.java class and MyWebService.java interface.
MyWebService.java is a java interface contains methods of the web service that a client can call.
MyWebService_Service.java is a java class that has URI and port of the web service and gives instance of MyWebService by getMyWebServicePort() method. So that client can call the method of MyWebService. We'll see this in next section.
We use the client [discussed in previous section] JAVA files to call our web service. You can call web service from anywhere by using client JAVA files. Just do the following [I use simplest technique]:
1. Click File - New Project (Ctrl + Shift + N) or click New Project on Tool Bar. Select Java under Category and Java Application under Projects and click Next
2. Give project name to WebServiceCall and click Finish.
3. Following files will be in Project Explorer.
4. Create a package com.mywebservice.client in following way. Because I've created web service client under this package name by using wsimport command in previous section.
5. Now go to our web service client JAVA files location. In my case, it is C:\Users\khan.ashik\com\mywebservice\client .
And copy all .java files (here 8 java files) and paste to newly created package in net beans.
6. The Projects explorer looks like the following
7. Double click on WebServiceCall.java to open the source code. And I've modified main function as follows.
public static void main(String[] args) {
// TODO code application logic here
// initialize the port
MyWebService_Service port = new MyWebService_Service();
//initialize the MyService interface to call the operations
MyWebService service = port.getMyWebServicePort();
// call the Addition operation with 3 int parameters and retrieves the result in a variable
int result = service.addition(1, 2, 3);
//print the result of web service
System.out.println(result);
}
8. Now right click on WebServiceCall.java and choose Run File. Or choose SHift+F6 to view the output.
9. The output should be 6.
And that's all about this topic.
1. Web Services contained in Application Server provides services through XML/JSON using HTTP protocol.
2. JAX-WS is JAVA API for standard web services which simplifies developing web services using Java Technology.
3. Here we use WSDL XML (Web service Descripto Language) based web service. And parse WSDL to generate Web clients.
No more theoretical discussion here. There are huge theoretical discussions on Web.
Now we are concentrating on CRASHING hands on. Our steps are as follows:
1. Our tools are: NetBeans 7, GlassFish Server 3.1.2, JAVA 6. [i.e. you can use later versions]
2. Create a JAVA Web Application. [i.e. you can use your existing web Application]
3. Create Web Service.
4. Adding Operations to the Web Service.
5. Deploying and Testing the Web Service.
6. Create Client for Web Service.
7. Use the Client to call web service.
1. Setting Up Environment
Download NetBeans from https://netbeans.org/downloads/
[NetBeans have aplication Server Tomcat & Glassfish.]
Check Servers from services tab in NetBeans. Here GlassFish & Tomcat are default servers. And I
manually added JBOSS for a different projects.
My Project is based on Glassfish. If you use GlassFish, then do the following otherwise ignore. In Tomcat you can call by client BUT can't test using Net Beans utility.
- Goto GLASSFISH_INSTALL/glassfish/domains/domain1/config/domain.xml
- Add the following JVM option (in bold). And Restart GlassFish to take effect.
</java-config> ... <jvm-options>-Djavax.xml.accessExternalSchema=all</jvm-options> </java-config>
The above modification you can do easily. You can see the path from the properties of GlassFish under Services tab.
Go to the highlighted path and open domain1/config/domail.xml. Add the above line in appropriate place seen in figure.
Restart the server if it is already run.
2. Create JAVA WEB APPLICATION
You can ignore this step if you already have any JAVA Web Application1. Click File - New Project (Ctrl + Shift + N) or click New Project on Tool Bar.
OR
2. Select Java Web under Category and Web Application under Projects and click Next.
3. Give Project Name to MyWebApp and keep the all other settings as default and click Next.
4. Select GlassFish Server and Java EE 6 Web and click Finish.
5. Following files will be created in Project Explorer. Index.jsp file contained Hello World content to check the web application.
6. Now check the web application by Run.
3. Create Web Service
1. Right click MyWebApp node and do the following.
2. Give name as MyWebService and package name as com.weservice as the following and click Finish. Don't use stateless session bean i.e. that is a huge topic which i'll cover in another few posts.
3. Now MyWebService.java under package com.webservice is added under Source Packages Node. And a new Web Services Node is added. Under this node, MyWebService node is added followed by hello:String node. hello is a default method created by Net Beans which return type String.
You can add as many possible web services in your web application by following the above three steps.
4. Adding Operations to the Web Service
1. To add a new operation, open MyWEbService.java and go to Design view. And click Add Operation.
Or right click MyWebService under Web Services node and choose Add Operation..
2. Here I create an operation Addition which return type is Integer and it has 3 parameters of type int. So I've done the following and click OK.
Now if you expand the MyWebService node under Web Services node, then you'll see the operations list in your web service MyWebService.
Or if you go Design view of MyWebService.java under Source Packages, then you'll see the pictorial view of all your methods/operations.
3. Now go to Source view of MyWebService.java under Source Packages, I've only modified the return statements of my newly created Addition operation/method. i.e.
return num1 + num2 + num3;
@WebService annotation, the service name specified explicitly. In later version or in JBOSS or GlassFish server, the annotation can be like
@WebService()
@Stateless()
@WebParam annotation defines mapping between parameters of operations of Web Services and WSDL entry.
5. Deploying and Testing the Web Service
1. Right click the project and choose Deploy. The IDE starts the application server, build the application and deploys it to the server. You can see the specific server log in output window.
2. Right click on MyWebService under Web Service node and choose Test Web Service
Give 1 2 3 in respective 3 text boxes and click Addition button, you'll see the following output. Or it can show exception if you give wrong input.
Now click Back button of browser. Click on WSDL File link. You'll show the following
Or you may go to http://localhost:8084/MyWebApp/MyWebService to see the web service details.
So your WSDL link is:
http://localhost:8084/MyWebApp/MyWebService?wsdl
For using your web service from remote host, replace localhost with IP Address.
http://10.80.190.60:8084/MyWebApp/MyWebService?wsdl
Now i'll use this WSDL link to create a web service client.
6. Create client for Web Service
You can create client from Net Beans. BUT i'll create client by universal way.1. Go to cmd and issue command
wsimport -keep -p com.mywebservice.client http://10.80.190.60:8084/MyWebApp/MyWebService?wsdl
- -keep option keeps the source files.
- -p option specifies target package
- com.mywebservice.client is the target package.
- http://10.80.190.60:8084/MyWebApp/MyWebService?wsdl is the location of wsdl.
The java and class files will be located in com/mywebservice/client under the current directory. [i.e. C:\Users\khan.ashik]
2. 8 Java and corresponding 8 class files are created in that location.
3. 2 important Files are: MyWebService_Service.java class and MyWebService.java interface.
MyWebService.java is a java interface contains methods of the web service that a client can call.
MyWebService_Service.java is a java class that has URI and port of the web service and gives instance of MyWebService by getMyWebServicePort() method. So that client can call the method of MyWebService. We'll see this in next section.
7. Use the client to call web service
We use the client [discussed in previous section] JAVA files to call our web service. You can call web service from anywhere by using client JAVA files. Just do the following [I use simplest technique]:
1. Click File - New Project (Ctrl + Shift + N) or click New Project on Tool Bar. Select Java under Category and Java Application under Projects and click Next
2. Give project name to WebServiceCall and click Finish.
3. Following files will be in Project Explorer.
4. Create a package com.mywebservice.client in following way. Because I've created web service client under this package name by using wsimport command in previous section.
5. Now go to our web service client JAVA files location. In my case, it is C:\Users\khan.ashik\com\mywebservice\client .
And copy all .java files (here 8 java files) and paste to newly created package in net beans.
6. The Projects explorer looks like the following
7. Double click on WebServiceCall.java to open the source code. And I've modified main function as follows.
public static void main(String[] args) {
// TODO code application logic here
// initialize the port
MyWebService_Service port = new MyWebService_Service();
//initialize the MyService interface to call the operations
MyWebService service = port.getMyWebServicePort();
// call the Addition operation with 3 int parameters and retrieves the result in a variable
int result = service.addition(1, 2, 3);
//print the result of web service
System.out.println(result);
}
8. Now right click on WebServiceCall.java and choose Run File. Or choose SHift+F6 to view the output.
9. The output should be 6.
And that's all about this topic.
Nice presentation style...helping me lot....pls complete the post quickly sir...
ReplyDeleteThanks for comments. Insha Allah ajk complete korbo. :D
ReplyDeleteEach steps graphical presentation easy to understand without reading more. Very clear to understand.
ReplyDeleteWaiting for next.
Well done.....
Thanks.
Very clear and simple to understand for the beginners. Thanks a lot for this useful post for us.
ReplyDeleteThanks vi...
ReplyDeleteI have successfully implemented this in my local PC using this excellent tutorial and got success by first try....Nice.....
ReplyDeleteNow, requesting you to show us how to call web service from PL/SQL.
Great work.
Thanks. I'll give that tutorial very soon. Keep in touch.
ReplyDeleteVery informative post ! There is a lot of information here that can help any business get started with a successful social networking campaign ! Pyramid Analytics
ReplyDelete