Web Services
Web Services
What is a Web Service?
It is an application component that can be accessed using Web protocols and data encoding mechanisms such as HTTP and XML.
In our context the application is housed inside a web container, but it can also be an EJB.
Web Service Technologies and Protocols The following standards and protocols are standards in Web Services
I SOAP, combines XML and MIME to create an extensible packaging format. Used to mean Simple Object Access Protocol but nowadays its just a name without meaning.
I The Web Services Description Language, WSDL is an XML vocabulary to describe Web Services.
I Universal Description, Discovery, and Integration (UDDI) provides a model for organizing, registering and accessing information about Web Services.
I The Web Service Flow Language (WSFL) and Web Service
Collaboration Language (WSCL) are concerned with describing the workow between services so relationships can be encapsulated as part of an application
I Electronic Business XML (ebXML) provides a framework for e-Commerce.
SOAP can be carried over dierent protocols but in a JavaEE environment we often use HTTP. A message contains XML-documents wrapped in an XML SOAP envelope that forms the body of an HTTP POST request. A SOAP message describes how the service invokation should be done. One way is to use RPC style invokation.
How to do this is descibed in JAX-WS, the Java API for XML Web services. To handle JAX-WS on the server-side, you need a piece of code that unpacks and parses the SOAP messages. It also invokes the service.
Technically JAX-WS works similar to RMI, i. e. uses interfaces as proxies for the real service.
We use WSDL to describe our service and to generate the stubs and interfaces that are needed.
Web Service Architecture End point service Service WSDL interface JavaEE server SOAP client
To build a service you need to:
I Write a Service Endpoint interface and implement it. I Compile the Java les
I Run the wsgen tool to produce the les needed for deployment I Package this into a WAR-le.
I Deploy the web service. I Write the client
I Use wsimport to generate things that are needed to connect to the service
I Compile the client
An example of a web service is:
package helloservice.endpoint; import javax.jws.WebMethod; import javax.jws.WebService; @WebService
public class Hello {
private String message = new String("Hello, "); @WebMethod
public String sayHello(String name) { return message + name + "."; }
Our client
package simpleclient;
import javax.xml.ws.WebServiceRef;
import helloservice.endpoint.HelloService; import helloservice.endpoint.Hello; public class HelloClient {
@WebServiceRef(wsdlLocation = "http://localhost:8080/helloservice/hello?wsdl") static HelloService service;
/**
* @param args the command line arguments */
public static void main(String[] args) { try {
HelloClient client = new HelloClient(); client.doTest(args);
}
catch (Exception ex) { ex.printStackTrace(); }
public void doTest(String[] args) { try { // Call Web Service Operation
System.out.println("Retrieving the port from the following service: " + service);
Hello port = service.getHelloPort();
System.out.println("Invoking the sayHello operation on the port."); String name;
if (args.length > 0) { name = args[0]; }
else {
name = "No Name"; }
String response = port.sayHello(name); System.out.println(response);
} catch (Exception ex) { ex.printStackTrace(); }
We can now deploy the webservice and run the test client in the application container
A RESTFUL Web Service use the HTTP methods directly and you assign web addresses to dierent parts of a component.
REST means Representational State Transfer, it is specied in Java EE 6 as the JAX-RS specication.
A RESTFUL Web Service is accessed using a URL, i. e. a standard web address.
When you call such a service you issue a HTTP request, a result is returned using the HTTP protocol.
A RESTFUL Webservice is implemented either as a an ordinary class or as a stateless session EJB A simple example can look like
@Path("/book")
public class BookResource { @GET
@Produces("text/plain") public String getBookTitle() {
return "H2G2"; }
You specify a path that is added to the path of your application.
There is one method that will handle an HTTP GET request. It will return a plain text answer.
A more advanced example
@Path("/books") @Stateless
@Produces({"application/xml","application/json"}) @Consumes({"application/xml","application/json"}) public class BookResource {
@Context
private UriInfo uriInfo;
@PersistenceContext(unitName="chapter1") private EntityManager em;
@GET
public List<Book> getAllBooks() {
Query query = em.createNamedQuery("findAllBooks"); List<Book> books = query.getResultList();
return books; }
@PUT
public Response createNewBook(JAXBElement<Book> bookJaxb) { Book book = bookJaxb.getValue();