Building
Web Services with JAX-WS
JAX-WS stands for Java API for XML Web Services. It’s a
technology for building web services and clients that communicate using XML. It
allows developers to write message-oriented as well as RPC (Remote Procedure
Call) oriented web services.
Soap is used as XML-based protocol in order to represent
web-service operation invocation in JAX-WS. 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. Although SOAP messages are complex, the
JAX-WS API hides this complexity from the application developer. JAX-WS is bundled
with JDK 1.6 and it’s part of the Java EE platform from Sun Microsystems. JAX-WS uses annotations which are introduced
in Java SE 5 and it helps to simplify the development and deployment of web
service clients and endpoints. It is making developers life easy in order to
develop Java web service.
For using the JAX-WS, the developer specifies the web service operations by defining methods in an interface written in the Java programming language. The developer also codes one or more classes that implement those methods specified in the interface. Client programs are also easy to code. A client creates a proxy (a local object representing the service) and then simply invokes methods on the proxy. 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.
The reference implementation of JAX-WS is
developed as an open source project and is part of project GlassFish,
an open source Java EE application server. It is called JAX-WS RI (RI for reference
implementation) and is said to be a production quality implementation (contrary
to the former reference implementation being a proof of concept). This
reference implementation is now part of the GlassFish Metro distribution
Steps for creating java web services using JAX-WS:-
- Create a java project using eclipse.
- Create a package and name it as com.gaurav.jaxws.services and create an interface with name GenericMethods.java and their implementation GenericMethodsImpl class in that package.
- Create a package and name it as com.gaurav.jaxws.endpoint and then create a java file with name GenericMethodsPublisher.java in that package.
- Place the corresponding codes of their files as available below:-
GenericMethods.java
package com.gaurav.jaxws.services;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.DOCUMENT)
public interface GenericMethods {
@WebMethod
public String getFactorial(String inputString);
@WebMethod
public String getPrimeNumber(String inputString);
@WebMethod
public String getOddAndEven(String inputString);
}
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.DOCUMENT)
public interface GenericMethods {
@WebMethod
public String getFactorial(String inputString);
@WebMethod
public String getPrimeNumber(String inputString);
@WebMethod
public String getOddAndEven(String inputString);
}
GenericMethodsImpl.java
package com.gaurav.jaxws.services;
import javax.jws.WebService;
//GenericMethods interface Implementation
@WebService(endpointInterface = "com.gaurav.jaxws.services.GenericMethods")
public class GenericMethodsImpl implements GenericMethods {
public boolean isParsableToInteger(String strValue) {
try {
Integer.parseInt(strValue);
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
@Override
public String getFactorial(String inputString) {
String factorialResult = "Not a Valid Number";
if (inputString == null || inputString.trim().length() == 0) {
return "";
}
if (isParsableToInteger(inputString)) {
Long number = Long.parseLong(inputString);
Long factorial = number;
for (Long i = (number - 1); i > 1; i--) {
factorial = factorial * i;
}
factorialResult = "Factorial of given number " + number + " is "
+ String.valueOf(factorial);
}
return factorialResult;
}
@Override
public String getPrimeNumber(String inputString) {
int result;
String returnValue = "Not a Valid Number";
boolean flag = true;
if (isParsableToInteger(inputString)) {
int number = Integer.parseInt(inputString);
for (int i = 2; i <= number / 2; i++) {
result = number % i;
if (result == 0) {
flag = false;
break;
}
}
if (flag)
returnValue = number + " is Prime Number";
else
returnValue = number + " is not Prime Number";
}
return returnValue;
}
@Override
public String getOddAndEven(String inputString) {
String returnValue = "Not a Valid Number";
if (isParsableToInteger(inputString)) {
int number = Integer.parseInt(inputString);
if (number % 2 == 0)
returnValue = number + " is even number";
else
returnValue = number + " is odd number";
}
return returnValue;
}
}
import javax.jws.WebService;
//GenericMethods interface Implementation
@WebService(endpointInterface = "com.gaurav.jaxws.services.GenericMethods")
public class GenericMethodsImpl implements GenericMethods {
public boolean isParsableToInteger(String strValue) {
try {
Integer.parseInt(strValue);
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
@Override
public String getFactorial(String inputString) {
String factorialResult = "Not a Valid Number";
if (inputString == null || inputString.trim().length() == 0) {
return "";
}
if (isParsableToInteger(inputString)) {
Long number = Long.parseLong(inputString);
Long factorial = number;
for (Long i = (number - 1); i > 1; i--) {
factorial = factorial * i;
}
factorialResult = "Factorial of given number " + number + " is "
+ String.valueOf(factorial);
}
return factorialResult;
}
@Override
public String getPrimeNumber(String inputString) {
int result;
String returnValue = "Not a Valid Number";
boolean flag = true;
if (isParsableToInteger(inputString)) {
int number = Integer.parseInt(inputString);
for (int i = 2; i <= number / 2; i++) {
result = number % i;
if (result == 0) {
flag = false;
break;
}
}
if (flag)
returnValue = number + " is Prime Number";
else
returnValue = number + " is not Prime Number";
}
return returnValue;
}
@Override
public String getOddAndEven(String inputString) {
String returnValue = "Not a Valid Number";
if (isParsableToInteger(inputString)) {
int number = Integer.parseInt(inputString);
if (number % 2 == 0)
returnValue = number + " is even number";
else
returnValue = number + " is odd number";
}
return returnValue;
}
}
GenericMethodsPublisher.java
package com.gaurav.jaxws.endpoint;
import javax.xml.ws.Endpoint;
import com.gaurav.jaxws.services.GenericMethodsImpl;
public class GenericMethodsPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:7070/jaxws/genericMethods",
new GenericMethodsImpl());
System.out.println(" Services are published successfully ");
}
}
import javax.xml.ws.Endpoint;
import com.gaurav.jaxws.services.GenericMethodsImpl;
public class GenericMethodsPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:7070/jaxws/genericMethods",
new GenericMethodsImpl());
System.out.println(" Services are published successfully ");
}
}
About Endpoint:-
- A Web service endpoint.
- Endpoints are created using the static methods defined in this class. An endpoint is always tied to one Binding and one implementor, both set at endpoint creation time.
- An endpoint is either in a published or an unpublished state. The publish methods can be used to start publishing an endpoint, at which point it starts accepting incoming requests.
static Endpoint
|
publish(String address,
Object implementor)
|
Creates and publishes an endpoint
for the specified implementor object at the given address.
JAX-WS Related Annotation meaning is
given below:-
- @WebService: - Marks a Java class as implementing a Web Service, or a Java interface as defining a Web Service interface.
- @WebMethod: - Customizes a method that is exposed as a Web Service operation.
- @SOAPBinding: - Specifies the mapping of the Web Service onto the SOAP message protocol.
- @WebParam:- Customizes the mapping of an individual parameter to a Web Service message part and XML element.
- @WebResult: - Customizes the mapping of the return value to a WSDL part and XML element.
Note:- In the GenericMethodsPublisher.java,
I am using java main method so we can execute this class by using Run
as Java application.After the execution of this class we will get
the WSDL file in the specified location i.e
http://localhost:7070/jaxws/genericMethods?wsdl. ScreenShot is attached below:-
Now, for the generation of client files we can use wsimport command which is available inside the bin directory of JDK installated location. So before using the wsimport command, we have to perform few
Steps:-
- SET path for jdk bin directory like set path=%path%;C:\Program Files\Java\jdk1.7.0_03\bin;
- Now go to the eclipse workspace location inside JAXWS-JavaWebService project location.
- Now use wsimport -keep http://localhost:7070/jaxws/genericMethods?wsdl command to generate the code.
Note:- This command(wsimport) is responsible to generate necessary client
files, which is based on the provided WSDL file. For the above project here it will generate
multiple files.
After the execution of wsimport command the generated classes are as follows:-
About Client Program:-
- Create a java project in eclipse.
- Create a package and name it with com.gaurav.jaxws.client and then create a java file named with GenericMethodsClient in that package.
- place the below source code in the java file GenericMethodsClient.java
- Add the required generated sources(created by wsimport command) while creating the client program.
package com.gaurav.jaxws.client;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.gaurav.jaxws.services.GenericMethods;
public class GenericMethodsClient {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:7070/jaxws/genericMethods?wsdl");
/**
* 1st argument refers to the namespace, we can check this in the WSDL
* file which is published in the
* http://localhost:7070/jaxws/genericMethods?wsdl location.
* 2nd argument is service name, we can check this in the WSDL file itself.
*/
QName qname = new QName("http://services.jaxws.gaurav.com/",
"GenericMethodsImplService");
Service service = Service.create(url, qname);
GenericMethods genericMethods = service.getPort(GenericMethods.class);
System.out.println(genericMethods.getFactorial("10"));
System.out.println(genericMethods.getPrimeNumber("11117"));
System.out.println(genericMethods.getOddAndEven("9888"));
}
}
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.gaurav.jaxws.services.GenericMethods;
public class GenericMethodsClient {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:7070/jaxws/genericMethods?wsdl");
/**
* 1st argument refers to the namespace, we can check this in the WSDL
* file which is published in the
* http://localhost:7070/jaxws/genericMethods?wsdl location.
* 2nd argument is service name, we can check this in the WSDL file itself.
*/
QName qname = new QName("http://services.jaxws.gaurav.com/",
"GenericMethodsImplService");
Service service = Service.create(url, qname);
GenericMethods genericMethods = service.getPort(GenericMethods.class);
System.out.println(genericMethods.getFactorial("10"));
System.out.println(genericMethods.getPrimeNumber("11117"));
System.out.println(genericMethods.getOddAndEven("9888"));
}
}
Result:-
Factorial of given number 10 is 3628800
11117 is Prime Number
9888 is even number
11117 is Prime Number
9888 is even number
ref taken from :- oracle.com and wikipedia.org
My blog goes over a lot of the same topics as yours, and I believe we could greatly benefit from each other. If you happen to be interested, feel free to shoot me an e-mail.
ReplyDeleteoccupational health and safety course in chennai