JAX-WS Web Services [Java API for XML Web Services (JAX-WS)]
What is JAX-WS web services |
On a technical level, web services can be implemented in various ways. The two types of web services discussed in this section can be distinguished as “big” web services and “RESTful” web services.
“Big” Web Services
In Java EE 6, JAX-WS provides the functionality for “big” web services, Big web services use XML messages that follow the Simple Object Access Protocol (SOAP) standard,
an XML language defining a message architecture and message formats. Such systems often contain a machine-readable description of the operations offered by the service, written in the Web Services Description Language (WSDL), an XML language for defining interfaces syntactically.
The SOAP message format and the WSDL interface definition language have gained widespread adoption.Many development tools, such as NetBeans IDE, can reduce the complexity of developing web service applications.
A SOAP-based design must include the following elements.
- A formal contract must be established to describe the interface that the web service offers. WSDL can be used to describe the details of the contract, which may include messages, operations, bindings, and the location of the web service. You may also process SOAP messages in a JAX-WS service without publishing a WSDL.
- The architecture must address complex nonfunctional requirements. Many web service specifications address such requirements and establish a common vocabulary for them. Examples include transactions, security, addressing, trust, coordination, and so on.
- The architecture needs to handle asynchronous processing and invocation. In such cases, the infrastructure provided by standards, such as Web Services Reliable Messaging (WSRM), and APIs, such as JAX-WS, with their client-side asynchronous invocation support, can be leveraged out of the box.
JAX-WS : addresses advanced QoS requirements commonly occurring in enterprise computing. When compared to JAX-RS, JAX-WS makes it easier to support the WS-* set of protocols, which provide standards for security and reliability, among other things, and interoperate with other WS-* conforming clients and servers.
JAX-WS web services Example |
HelloWorld.java
package com.javaskool.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{
@WebMethod
String sayHello(String name); //web service method
}
HelloWorldImpl.java
package com.javaskool.ws;
import javax.jws.WebService;
//Service Implementation
@WebService(endpointInterface = "com.javaskool.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@Override
public String sayHello(String name) {
return "JAX-WS : Hello Mr. " + name;
}
}
HelloWorldPublisher.java
package com.javaskool.endpoint;
import javax.xml.ws.Endpoint;
import com.javaskool.ws.HelloWorldImpl;
//Endpoint publisher
public class HelloWorldPublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/ws/myhello", new HelloWorldImpl());
}
}
HelloWorldClient.java
package com.javaskool.client;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebServiceRef;
import com.javaskool.ws.HelloWorld;
public class HelloWorldClient{
// @WebServiceRef(wsdlLocation = "http://localhost:8912/ws/myhello?wsdl")
// private static HelloWorld service;
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/ws/myhello?wsdl");
//1st parameter is service URI, refers to wsdl document above
//2nd parameter is service name, refers to wsdl document above
QName qname = new QName("http://ws.javaskool.com/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
System.out.println(hello.sayHello("James"));
}
}
Output
JAX-WS : Hello Mr. James
Web Services details
WSDL details
Recent Comments