Web Services - Java
Updated:
- Definition of Web Service
- Available libraries
- Writing a client
- Developing a server using JAX-WS
- Summary
Definition of Web Service
- Comes from w3c group and has to do with standardisation and interoperability. It is a business logic and it provides some functionalities, but in order to use it you need an interface.
- The interface is usually described through XML – specifically in WSDL – and web services could be accessed using mechanisms – SOAP messages.
- And we invoke them btwn client and server, server takes the msg, and sends back the reply
- WSDL is the tool to define service specification. It’s a brain that explains how to access and use the interface and web service
The case for SOAP
- Before amazon became famous, all the web services were in SOAP. Amazon exposed the information and functionality through web services. E.g. searching, shopping
- If you were to use amazon, you ask amazon directly ‘can I have a copy WSDL’ – no broker needed. The WSDL document is generated using a tool.
- Useful if you want to write a program – I program some code, and the code becomes a client and it interacts with web service, supported by amazon.
What do we need?
- If I were to use it I need to think about 3 diff levels :
- Low-level – need to construct a SOAP message to send from client to amazon server – about parsing, XML, JSON
- Mid-level – once I think about the core of the communication – SOAP message needs to be assembled. e.g. this is the method I’d like to invoke in amazon server. How do I do that? (construction of the message)
- High-level – use SOAP concealed behind the interface – once I am able to think about SOAP msg constructed, I need to send that to the server (invocation of the service)
- Some means of deploying the service – we could use it through a web container like Tomcat, given tendency for HTTP being the default for transport mechanism for SOAP. But it could be SMTP, etc
Available libraries
- JEE – web services are already integrated there
- And number of major vendors like amazon, Microsoft, google and they provide their own comprehensive development kit
- There are other solutions – Apache – open source called soap
implementation of Axis– a bit old to use.
- What it does is its all to do with standards- you take the standard APIs like JAX-RPC and implement the standard and make it available to us as a tool to use it
- Oracle has integrated Web Services Stack to develop Web services
technology APIs and tools
- JAX-RPC – ideal to invoke soap messages, and is available at JWSDP
- JAX XS – Java API for XML web services at JAX-WS
Service provision and consumption
- Refer to prev lecture [Note 6 – SOA, Service Provision and Consumption]
- → Whole vision on service provision and consumption
Stubs
- Important is we still think about client being able to send a rq to server, and we need an interface! → Stub/ Proxy
- These clients will need to marshall the msg, and when it gets to
server its unmarshall
- This is bc most Web Services operate in a heterogeneous environment, so we need to package the data into common format.
- Server side needs the same interface and it will be collection of routine – receive- unmarshall- service – reply
- Very important that this concept of marshall and unmarshalling and serialisation and deserialisation is apeopleied here → same thing as RPC
WSDL, Stubs, Marshalling
- The SOAP client will get the WSDL from the server, and it becomes available in the client side → then client can use it to send a request and reply.
- We’ve seen this before - It’s about RPC!!
- Client process will invoke a function called ‘Doit’ – and it has 2 params a,b, and client process will take ‘Doit’ passes 2 value, marshalls as a msg and send it, server receive it and unmarshalls, do it and send reply back.
- Once you have it, u’ll have to generate a stub – how do I do this?
- E.g.
- He has a copy of WSDL document, and he’s written a script
- Use a tool called WSimport, using this can you create a hierarchy of directory, and then go and get WSDL document from this URL and it creates a stub for me.
- It created amazon folder, and there are number of java files automatically from WSDL and have been compiled.
- If they’ve been compiled, can I have stubs in a jar file? Anything that’s a class, can you make sure its in my jar file?
- Am ready to write a client – need 3 lines of code.
Writing a client
- Get a WSDL document
- I have used a tool such as WSimport to create directories and java code was automatically created and made sure its compiled. – stubs
- We got the stub
- For my client:
- To look at the locator class – you need have an instance of class saying I’m gonna create a service
- U get an instance of the Soap class by calling appropriate method – how do you build a Soap msg for that service – called SOAP class.
- Invoke the desired method of the Soap object - I know amazon has a method, and I’m gonna invoke it on my soap object
Extra e.g. A very few soap services available
- Yodaspeak.co.uk
- He has to look at the WSDL document available in Minerva
- What he’s gonna do is to generate the stubs – those are in folder
yoda – with 2 java files and are compiled. He’s going to look at the
2 files that are part of the stub – and ensure they are in packages.
- Port type – locator
- Other one - Instance of the actual class
- Client will simply use 3 lines of code
- Create SOAP URL – web service is here
- Qualified name – so I’m comfy with XML – to do with actual location of the service
- To use the stub – ur looking at port type ‘yodatalk’ and is from the stub
- Then you invoke the method yodatalk. – which again comes from the stub itself
Developing a server using JAX-WS
- Use JAX -WS to develop a server
- Create an WS-Service Endpoint interface and give it a name.
- Is called HelloWorld 2. Then ur gonna implement the functionality
- It will return hello world JAX-WS and name
- Endpoint Interface – this is where you find the hello world 3. Once I have the implementation, I need to publish the interface
- Publish it on this URL
- Endpoint.publish - that’s where my web service is up and running.
- And you can check if the WSDL is published by accessing the URL
- Two ways to develop. A client using JAX-WS: 1. No tool – hard code it. I know where the WSDL is. Create URL and get the document. Then you do qualified name where you pass the service location. And you create the service and invoke it with getPort. 2. With wsimport - it takes the WSDL and generate stubs, and use stubs to invoke the service. - → Easier to use stubs with the client
Summary
- Noted that standard libraries and third-party libraries provide various levels of support for using Web Services in Java
- Examined how high-level APIs hide the messy details of SOAP message handling – transparent to us
- Seen how server and client can be implemented using JAX-WS.
Leave a comment