Creating Code First(Bottom-UP approach) Web Service:
I have been talking about bottom up approach for creating web services in this post with using eclipse.
Bottom up we need to write first java code like beans or enterprise beans where as In contract first ,
We need to define contract like XSD,Schema and wsdl.
Setup Eclipse for developing web services in java:
I have been talking about bottom up approach for creating web services in this post with using eclipse.
Bottom up we need to write first java code like beans or enterprise beans where as In contract first ,
We need to define contract like XSD,Schema and wsdl.
Setup Eclipse for developing web services in java:
We need to different software setup like
JDK,Web Server or App Server,WTP,etc.
Prerequisite
Installation:
Install JDK
Install Web Server or App Server like
Tomcat, boss.
Install Eclipse like Indigo, Helios, etc.
Setup:
1-When Eclipse is installed then you can
check your eclipse preferences (Windows
-> Preferences) where Web Service
preference is present in the list or not.
If Web Service preference is not there then you need to plugin WTP
(Web Tools Platform) after plugin Web Services link would be available in the
eclipse. Link for Helios plugin http://download.eclipse.org/webtools/repository/helios/
Click Finish button and get the added
CXF runtime.then apply and ok
Product.java
package com.dtechtalkcenter.service;
public class Product {
private String productName;
private String productCategory;
private String productDetails;
private double cast;
public Product()
{
}
public Product(String productName, String productCategory,
String productDetails, double cast) {
super();
this.productName = productName;
this.productCategory = productCategory;
this.productDetails = productDetails;
this.cast = cast;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductCategory() {
return productCategory;
}
public void setProductCategory(String productCategory) {
this.productCategory = productCategory;
}
public String getProductDetails() {
return productDetails;
}
public void setProductDetails(String productDetails) {
this.productDetails = productDetails;
}
public double getCast() {
return cast;
}
public void setCast(double cast) {
this.cast = cast;
}
}
/*It is interface which has two methods.
* that would be available in the service as operation
*/
package com.dtechtalkcenter.service;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface ProductService {
//Return List of product
List<Product> getProducts();
//add the product into the list
void addProduct(@WebParam(name="product")Product product);
}
5-Select runtime: Select runtime as per require. I have taken the CXF
runtime
Then,we can select web service type in the combo like Bottom up java bean service and Service Implementation java class.
2-Configure installed JRE in eclipse
(Windows -> Preferences -> Java -> Installed JREs)
3-Setup
CXF or Axis2 runtime: In eclipse go to Windows
-> Preferences
And select
CXF or Apache Axis2 as per runtime adds runtime to eclipse for creating web
services.
I have taken Apache CXF for creating
service so I select the CXF preference.
Click on Add button and browse the Apache CXF runtime.
Creating a Web service: Now, we can start to create web service on eclipse using Apache CXF
runtime.
Steps:
1-Create a dynamics web project in Eclipse.
a-
project name ex. StandardWebService
b- Select Runtime like apache tomcat v7.0
c- Click on modify button for setting facts.
b- Select Runtime like apache tomcat v7.0
c- Click on modify button for setting facts.
2-Select Apache CXF runtime
Click on modify button, you can select the
CXF check box for this project.
Here we can select Apache cxf or apache
axis as engine for creating service. Now i am using apache cxf engine for
creating service.
3-Write the Java classes which you want create
as service:
I have written these classes as sample like
a-
Product
b-
ProductService (it is
interface, it has two methods)
c-
ProductServiceImpl (it is
implementation of product service)
Product.java
package com.dtechtalkcenter.service;
public class Product {
private String productName;
private String productCategory;
private String productDetails;
private double cast;
public Product()
{
}
public Product(String productName, String productCategory,
String productDetails, double cast) {
super();
this.productName = productName;
this.productCategory = productCategory;
this.productDetails = productDetails;
this.cast = cast;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductCategory() {
return productCategory;
}
public void setProductCategory(String productCategory) {
this.productCategory = productCategory;
}
public String getProductDetails() {
return productDetails;
}
public void setProductDetails(String productDetails) {
this.productDetails = productDetails;
}
public double getCast() {
return cast;
}
public void setCast(double cast) {
this.cast = cast;
}
}
ProductService.java interface
/*It is interface which has two methods.
* that would be available in the service as operation
*/
package com.dtechtalkcenter.service;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface ProductService {
//Return List of product
List<Product> getProducts();
//add the product into the list
void addProduct(@WebParam(name="product")Product product);
}
ProductServiceImpl .java
package com.dtechtalkcenter.service;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
@WebService(endpointInterface ="com.dtechtalkcenter.service.ProductService")
public class ProductServiceImpl implements ProductService{
@Override
public List<Product> getProducts() {
List<Product> product=new ArrayList<Product>();
product.add(new Product("Windows","OS", "windows server", 300));
product.add(new Product("Linux","OS", "Linux server", 100));
product.add(new Product("Mac","OS", "Mac server", 500));
return product;
}
@Override
public void addProduct(Product product) {
System.out.println(product);
}
}
4-Create web service of ProductServiceImpl
Java Code:
For Creating the Service of this java class, Step follows
For Creating the Service of this java class, Step follows
(Right
Click on project>New>Other) and select web service wizard.
Click on the next button and default,
Apache runtime would be Axis.
Change the runtime as per require like CXF
or Axis2, click on web service running link.
Click on OK button
Then,we can select web service type in the combo like Bottom up java bean service and Service Implementation java class.
Click on next button:
Select an existing Service Endpoint
Interface (SEI) or create an SEI by extracting an interface from the
implementation class.
6-Click
finish button of eclipse:à
It is created a service and deployed on
tomcat.
Then We can see the all services of this
project
URL
:( http://host:port/projectName/services) http://localhost:8080/StandardWebService/services
7-WSDL
URL of Service:
You can see the wsdl document of the
service.
URL: http://localhost:8080/StandardWebService/services/ProductServiceImplPort?wsdl
8-Client
of Service: You can generate the client using
eclipse or can use soap ui tool for using the service.
If you can view WSDL and XSD, you are finish to deploy Web Service on application server. We can use soapUI to simulate Web Service Client
Download address: http://sourceforge.net/projects/soapui/files/
Please select installable file to download. When you installed sopaUI, please follow below step to simulate Web Service Client.
File -> New soapUI Project
Project name : StandardWebService
and click ok button.
Now, you can see two web service function name in the tree. Open addProduct and getProducts to get request 1. Left window will display below XML content:
click on the request of service and just submit the request through soap ui to service.
No comments:
Post a Comment