• No results found

Web Services ( )

N/A
N/A
Protected

Academic year: 2021

Share "Web Services ( )"

Copied!
16
0
0

Loading.... (view fulltext now)

Full text

(1)

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.

(2)

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.

(3)

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.

(4)

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.

(5)

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.

(6)

Web Service Architecture End point service Service WSDL interface JavaEE server SOAP client

(7)

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

(8)

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 + "."; }

(9)

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(); }

(10)

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(); }

(11)

We can now deploy the webservice and run the test client in the application container

(12)

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.

(13)

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.

(14)

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"; }

(15)

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.

(16)

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();

References

Related documents

Health risk behaviors are associated with demographic and job characteristics such as gender, age group, job, or work type (21,22]. Therefore, we need to consider the

Thus, a longer follow-up should be used to evaluate the long term evolution of these two treatments, but both treat- ments can be considered in cases of patellar tendinopathies

We present three algorithms; the first algorithm Iterative is efficient in reconstructing the main diago- nals in case the nodes corresponding to the hidden M are such, that they

This forum is dedicated to the discussion on the social values of Second Language Acquisition (SLA) research and is motivated by talks organized in the Fall of 2018 by Columbia

Late maternal death is defined as a maternal death due to pregnancy (direct or indirect obstetric causes) which occurred more than 42 days but less than one year after the end

In view of the additional information provided and of the reported relevance of the relations for the work on medical devices, and in order to encourage

We will become a vigorous rural area whose inhabitants have a sense of community and co-operate across all sec- toral and municipal borders, while our commitment and knowledge

There is enough work for the Catholic Church attempt to influence the Healthcare policy to modify the financial system and to support research projects they will develop a new