Introducing SOAP and JAX-WS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you this is the start of part 2 of the course where our focus will shift to soap based Web Services remember this isn't just a spring course I'm assuming no previous knowledge of web services so in this chapter we'll do the groundwork and cover the basics there won't be any spring in this chapter we'll start with a high-level overview of soap I'll keep this really basic for now so that we've got all the fundamentals perhaps the most important of basics is the interestingly named was still so we'll spend some time on understanding that but the biggest part of this chapter will be concentrating on jax-ws this is a standard built in Java library for building web services and it actually makes the process quite simple if you've already worked with jax-ws before then you can safely skip this chapter but if not let's begin we see now that Java features built in a library called RMI and that's great it's pretty easy to use but the restrictions are that both the clients and the server must be written in Java oh and also firewalls can be a problem because the protocol between the two sides is a custom protocol invented by Java if you did the chapters on hessian burlap and HTTP 10 boca you'll have seen that we can get around the firewall problem because these solutions use HTTP but still the client and server will have to be written in Java and the real problem with those three solutions is that they're kind of non-standard they're quite obscure in the early 1990s and the early 2000s I guess some efforts were made by various bodies to create some kind of remoting standard that would be language and operating system neutral you might have heard of tawbah that was the 1990s efforts but that wasn't all that successful it did have some success but it didn't set the world alight because I would say it was very complicated and expensive to implement you need a special software to make it work and that software was often expensive after core became web services and specifically a protocol called soap now there is another type of web service called rest which will be studying later but for now I'm going to use the terms soap and web service interchangeably soap was invented by a team at Microsoft and it went on to become much more successful than korba it is still a bit complicated but it was much kind of freer in that you don't need much in the way of special software to make it work it uses the well understood of well used standards of HTTP and XML for the communication if you're interested let's get this out of the way first so originally stood for simple object access protocol but it's one of those contrived acronyms that didn't really mean anything for example the S bit for simple is pretty inaccurate as really to be honest soap isn't very simple at all it is simpler than korba but there are quite a lot of complexities in soap as you're going to find out in this chapter but although it was invented at Microsoft it was very quickly transferred to the w3c committee they're the people in charge of maintaining standards for the world wide web so you can think of it as a fairly free and open standard and you certainly don't have to pay Microsoft or anybody else any money to write a web service so let's start with a simplified view of how soap web services work now as this chapter is concentrating on the basics of soap I will leave some details out but I hope it gives you the general idea of how things work so over here on the left-hand side we have a client's and crucially this can be written in any language it could be Python it could be C++ Visual Basic Java whatever over here on the right-hand side we have the server code now for us this is going to be Java so I have here for example a Java service class called employee management service but once again it didn't have to be Java it could have been any code and any language and the way that soap works is the client needs to be able to invoke a network call using HTTP now I know I've been saying the client could be written in any language what I really mean is that language needs to be able to make HTTP calls so really I mean most languages as most languages will be able to do this this is a quick digression it doesn't actually have to be HTTP you can for example use something like SMTP which is the emailing protocol which means you could send an email to the web service and get a reply back as an email now that's great but really that's a relatively obscure way of working in web services if at least at first you think of web services as being operations invoked using HTTP then you won't be far wrong now for the second part the requests that we're going to make to the server will be coded up in the form of an XML message now this has a lot of advantages the main one being that it's really easy to construct this message because it's not some kind of complex binary formats once again any language will be able to construct xml messages or at least as long as that language is capable of generating strings which after all is all an XML document is now the form of this XML document and I am heavily simplifying here but the concept of it is that it will look something like this the message is asking for the operation that we want to be performed in this case get employee by ID and then embedded in the message is the data that the operation needs the input parameters so for example here we have an employee with an ID of 7 3 8 so this data is going to be sent to the server and I'm going to call this the message and once the server receives that message the server can now process it and I hope you get the idea the server code can be any language as long as it's capable of constructing a string in response and that string is going to be another XML document this time the response here and it might look like this and this is the data that the client requested in this case it's the details of that particular employee now quick note here on the term web service it's a really misleading term for many people because this whole set up has got nothing to do with the web there's no browser involved here there's no web pages going on there's no HTML it's code on the left hand side on the client side and it's code on the right hand side for the server and now for a really important term in web services and that is WSDL it stands for web service description language you might sometimes see it referred to as web services definition language but more importantly we usually pronounce this as was dull and the idea behind a word still is that well imagine you have been given the job of writing a client application that is going to access some web service we didn't write that web service it may be owned by a third party company how do you know how it works how do you know what operations are available and how do you know what XML data is required in the messages of course you could ask the company who built that service and maybe they could supply you with some kind of a reference manual but the real answer is we use the word still you can think of the wisdom as being a kind of online reference manual for the web service but it's not a human readable reference manual it's more a machine readable reference manual and our client can use tools to access and understand that was dull so the wisdoms going to contain a list of all of the operations in the service together with a specification of what parameters are required in the messages so you can think of the wisdom as being a specification of the web service if you like a contract definition telling the clients how to call the service now this wisdom is an XML document itself and this caption here is showing a heavily simplified example real whistles are much more complicated than this and we will see a proper one later and we'll even be writing one for ourselves but I've really distilled the wisdom down to the important information so if the web service has or was de like this it's telling us that there are two types of messages that we can send and receive the first message here is called get employee by ID request and looking at the tag inside it's telling us that there is one if you like parameter that the wisdom calls it apart but I'm going to call it a parameter and that's called ID which needs to be of type integer there's another type of message called get employee by ID response this one having two parts or parameters the first name and surname both of which are strings I think on the caption earlier I had a few other fields but of course they would just be added as well but those two tags just show us the types of messages that can be sent and received by the web service this block down here is the full description of what the web service can do we'll be seeing this phrase ports quite a lot as we get a bit deeper with our whistles if you want to think of a porters just being the web service itself then you won't go far wrong so this tag here is saying that the web service is called the employee management web service and looking inside that tag we're going to see a series of operations I only have one operation of course there would be more than this generally but the one operation here is called get employee by ID so I hope that makes sense this is one of the operations that we can carry out on this web service it's telling us that the inputs for the get employee by ID service is the get employee by ID request message that's the message work that was defined higher up in the wisdom and the output from this operation is going to be get employee by ideal response and hope you can see that by doing it this way that means that these messages can potentially be reused by other operations on the web service so that's a very simple description of what a whistle is going to look like as I said we'll see a proper one quite soon so that's the basics of web services but how do we actually write one well I want to make a really important point here and that is we don't need soap or any special libraries to do web server in Java and that's because for a long time now Java has featured built into the library a sub library called jax-ws it's another not particularly nice acronym it actually stands for the Java API for XML based Web Services well that's a bit of a mouthful but you can just think of it as the java api for soap web services the key thing is it is built into java no special downloads are needed now we're going to see when we go a little bit deeper with soap web services that spring is going to help us a lot but you don't need spring in order to do a web service and in order to illustrate that i'm going to go back into eclipse now and together we can use jax-ws just to see the basics of how to write a working operating web service using jax-ws and no special libraries and that will help us to establish the principles oh just before I go there though I should mention that in earlier versions of Java that was an API called Jack's RPC that was the Java API for xml-based remotes procedure calls it was a very confusing acronym that but actually that was an API for accessing soap based web services but that API has now been deprecated it's been left behind by Java and it has been fully replaced by jax-ws but you never know you might come across some references to Jack's RPC and you might think you're missing out on something but you're really not jax-ws is where the action is the two key features of jax-ws firstly we're going to be able to write our service class using regular plain Java but using jax-ws will be able to automatically convert that class into a proper functioning web service without needing to deal with any XML at all and also jax-ws will generate the wisdom for us the second and this is coming from the client side of things if you're writing a client's application to a web service then jax-ws will enable you to write your Java calls without needing to ever even look at the word still which can be really valuable in some situations I must point out this automatic generation of the wisdom it can be a good thing and it certainly makes life a lot easier when you're getting started with web services but some web service experts think that you should write the was still manually and if you're writing the client they think that you should inspect the word still manually and there's a lot of credit in that point of view but in this chapter and the chapter that's following where we're going to be using spring with soap we are going to generate the wisdoms automatically in a later chapter we'll come back and have a look at how you could do things by hand if you really wanted to so for the rest of this chapter we're going to experiment with jax-ws to write a very basic web service there will be no spring involved here at all and to keep things clean I recommend that you close all your existing files and I'm even going to temporarily close our CRM system project and our clients application and I'm going to build a completely new project nice and clean and I'm going to call this the jax-ws test server now notice we've got no special libraries in this project it is a completely fresh java project i'm going to start by defining a new interface and this interface is going to be the usual service interface i think just for this test project i'm going to go with the employee management example and of course in the next chapter we'll apply this to our customer relationship management system let's just call this interface employee service and rather like on the caption so I'm going to have in here a method which is going to return an employee object and it's going to be called get employee by ID and it's going to take in a single string parameter which is the ID now of course for this project we don't have an employee class so I'll left click on the error marker here and go for the quick fix of creating the class employee and I really should be using packages here so I'm going to call the package employee management dot the main because this is going to be the main class called employee and we'll keep this simple we'll just have string in here called ID and we'll have a name and of course in real life we would have a lot more fields than that I've just realised they should be privates really if we're doing proper good Java encapsulation now I'm not going to do anything very clever in here I'm just going to have a constructor so I'll do source and generate constructor using fields you and I'm also going to put in here a two string method which is just going to return and will just return the name of that employee of course in real life there will be a lot more methods in here now since I created a package for the domain then I really ought to create a package for the services as well now you might not be really wanting to bother creating packages I strongly recommend that you create a package here I'm going to call it services employee management services you're going to see when we start generating the wisdoms and so on that packages are really important when it comes to soap based Web Services so I'll drag the employee's service class into there now before I forget I'm going to go back to the employee class and it's important to have a get and a set method for each of the each of the fields in here if you forget to do that then you'll notice that when we go further with our web service you'll find that the parameters don't appear in for example the wisdom gets generated it needs to get and set methods in order to be able to in order to be able to add them to the wisdom so let's add those in now we'll do a source and generate getters and setters make sure they're both ticked and click on OK and it's just added those to the bottom of the class I'm always forgetting to do that when I'm working on web services and then I'll discover a little bit later on down the line that for some reason why ID name aren't appearing in the web service if that happens to you then you'll know you need a pair of get and sets for each field that you want to be part of the employee next job then is to implement this interface and actually before I move on I should point out that I often forget this in jax-ws you don't need this interface it's just a habit really that led me to write this interface class there's certainly no requirement to have one in Web Services in Java I could have just gone ahead and written the implementation class but that's no big deal I'm going to have a new class in the services package this is going to be called employee service in pull it's going to need to implement the interface so I'll add the employee service interface right there and notice I've got the inherited abstract methods box ticked so I get a stub implementation of that method now because we're just testing here I'm not going to do anything clever in this service I'm going to return a hard-coded employee that employees going to have an ID of one and a name of the Chester word of course in real life we have a proper implementation in here and that's exactly what we'll be doing when we go over to spring I need to be able to convert this into a web service therefore I need a word still describing what's inside here and I need to set up some infrastructure code that's going to convert the incoming XML messages into the right formats and I also need some kind of infrastructure code which is going to convert this employee Java object that I'm returning here into an XML response message so that the clients can consume it and to achieve all of that all I need to do index WS is add on single annotation at web service and notice it is a capital S on the service I always forget that and in fact in an earlier take I got that completely wrong as long as you've spelt that correctly then a ctrl shift and O will yet will import the web service annotation and I want to make this point again I think it's it's quite an important one is that remember we have no special libraries in this project but web services are fully built-in to modern Java development kits and well that's it that's all we have to do to make this class be a web service of course we have to somehow publish this web service now the usual process for publishing a web service will be to take this class and to upload it to a server like Tomcat now that's exactly what we're going to do in later chapters especially when we start using spring but in jax-ws there is a very useful quick and dirty web server that you can use or at least there is if you're using the standard Sun Java development kit this feature of a sort of built-in quick and dirty web server is not a part the jax-ws standard so it depends on which implementation you're using as to whether you get it so if you're in the position of using a different virtual machine for instance the open JDK then this might not work for you but I'm assuming that most of you are using the standard Sun JDK so the following steps should work if it doesn't don't panic because you can just watch from this point onwards in the next chapter we'll be deploying to a proper web server but what we can do is we can write a quick class which I'm going to call the exporter class you can call it anything you like and I wish I picked the box we need a public static void main method usual format we're going to make a call on a class called endpoints now can't import that just yet because I haven't called any methods on it but if you hit the dots depending on the version of Eclipse you're in it's kind of guessing which endpoint class we're talking about we're going to call the method called publish so if you can just select publish from the list the the thing I want to point you towards is notice the import there which was automatically added when we selected publish the import is coming from the Java X dot XML ws package which is in fact jax-ws the first parameter is the URL that we want this service to be accessed from now this is something that I haven't mentioned so far I didn't mention it in my brief overview but every web service because it's accessible through HTTP is going to have a URL now that URL is a regular web style URL so it's going to begin with HTTP colon forward slash forward slash and now we would use the IP address of the server that we're deploying to now because we're not really deploying this we can use the stock IP address of localhost you probably know that mean just the IP address that this particular computer is running on we generally use a ports when we're developing so I'm going to use the port of 8080 which is quite a common port to be using or by the way before you run this application if you're still running Tomcat from an earlier chapter make sure you close Tomcat down otherwise where we're going to clash with Tomcat but the interesting thing is that because we're kind of in control of this little webserver that we were setting up we can now use any pattern we like and I'm going to emphasize that by putting any name you like separated by forward slashes and I'm going to end it with let's say employee service now of course that's a silly URL but I want to do that just to just to really emphasize that this URL isn't being automatically generated at any stage we had complete control of what that URL should be we need to remember this URL and in real life we would be giving our third-party customers or the people who want to call the web service we would be giving them this URL now the second parameter is a simple one this needs to be an instance of the class that we want to become a web service so in our case that's employee service in pull we need to instantiate employee service input I sometimes forget that I need a new instance there sometimes I accidentally do employee service import class you will get an exception if you do that but that's it and one of the things I love about jax-ws is just with this line of code that's going to take our employee service in pull and it's going to wrap around all of that XML based plumbing code that's going to make it a true full-fledged standards-compliant web service I'll just say one more time though that this line of code will only work if your implementation of jax-ws support endpoint publishing that really means that as long as you're using the Sun virtual machine I should say now the Oracle JDK then this will be fine if not then you're just going to have to watch and you can wait until the next chapter when we will deploy our web services to Tomcat but all being well if I now do a right-click and a run as java application you're going to see that apparently nothing happens if you have got any typos you might get an exception here but assuming there's no exceptions I'm I'm waffling deliberately here just to give it some time that does appear to be up and running so now effectively what we have is a little mini HTTP server sat waiting for requests and the next thing now would be for somebody maybe a third party to write a client's application to connect to our web service so we'll do just that in a few moments but the obvious thing to try is it would be interesting to see what happens if we put this URL into a web browser now before I do that I want to be clear about this you don't access web services using web browsers you know that from the discussion we had just a few moments ago we're going to access the web service using code but because this is using HTTP if we try to access this service at this URL we might see something just to give us a clue that things are working so I just copied that string from the URL in the code and I've just pasted it in here of course it was a ridiculous URL if I hit enter then this is interesting now what we're seeing here is effectively just a test web page the implementation of jax-ws has decided that if you access this address through a web browser what you'll see is some basic information about the web service it really is very basic implementation but there is one really interesting piece of information on this page and that's the link you can see here that's a link so the Worstell now remember we never wrote or was still but one of Jack's WSS jobs is to automatically generate one if you have a look at the URL you'll see actually it's the URL of the web service followed by question mark we're still and that's the pattern that that jax-ws always follows let's click the link to that and I did warn you that the real whiz dolls are significantly more complicated than the example one I gave you on a caption before but you should be able to see just by scanning down this that you recognize some of the elements for example here you can see the definition of a message called get employee by ID there's one there called get employee by ID response the difference is though you'll notice that inside that message the the part does reference parameters but doesn't actually show any of the parameters we expect an ID in here and the response should have an ID and a name now this is getting a little bit more advanced but the reason for this is that the definition of the messages are in a separate file you can find that separate file here this URL is pointing to what's called the XS D which is a standard XML term meaning a schema definition now if you don't know what that means then we will be covering that in a later chapter you don't really need to know it for now but just in case you're interested I'll paste that link into the browser for example we can see that the definition of an employee is that it should have an ID and a name so if we go back let's have a look at some of the other detail we have there the port type which remember is really the name of the web service and the name the default name of the web service is just the name of the class that defined it so employee service input you can change the name of the web service by adding that as a parameter to the web service annotation actually but I want bother doing that I'll go with the defaults inside the port we have one single operation called get employee by ID of course if we add more methods into that Java class then further operations will be added here and we can see that there's an input and an output to that operation I'm not going to look in any more detail at what follows the binding is a little bit more complicated the service I guess is is quite interesting that's just taught that's really just containing the URL of the actual service itself now we already know what that is because it's very similar to the URL of the wisdom but sometimes that address can be completely different so that's always still not very pleasant to look at I must admit and it's quite good that it's been generated for us but it's really good to know that it's there that proves our web service is deployed and it's up and running ok so we have now written and published a working web service we've done all of the work over here on the right hand side but now we're going to look at what things are like if your job is to write a client's for this application now remember from our discussion before that what the client needs to do is it needs to construct an XML message of the type you can see here of the kind of structure you can see at the top and it needs to send that message across to the server and you know now that that's going to be to the URL that we defined earlier by the way if you're interested in a bit more detail you're going to send this message via a HTTP POST if you have no idea what I'm talking about there then check out the chapter on rest based web services where we'll be looking at posts in a lot of detail if you're happy just to accept that the client has to send this XML message to the web service in order to get back an XML response remember that the client could be written in any language as long as they can send XML messages it could be Python C++ Visual Basic or whatever but the question I want to ask is if we're a Java programmer and we want to call a web service any web service how do we do it using jax-ws so what we'll do in common with all of the other chapters on remoting is i'm going to suggest that you temporarily close the projects but our jax-ws test server but I want you to remember that the server is still running do not press the red terminate button we need that service to be up and running but I want you to pretend now that you are a client developer you have no visibility whatsoever of that server code you don't know what language it's written in you don't know how its implemented what you've been told that you need to access the web service at the URL you can see here how do we do it well let's first of all create a new project for this so it's going to be let's call it jax-ws test clients and I'm going to write a new class in here and I'll just call it Maine I'm going to generate a public static void main method my objective in here is to call that web service the only thing we know about this web service at the moment is its URL is and I think I've still got the URL on my clipboard yes I do the URL or at least the URL of the wisdom is the address you can see here where do I start well the way jax-ws works is it supplies a special tool it's a tool that's part of your Java development kit and that tool is supplied the name of the wisdom and the tool will then generate a set of classes representing that web service now remember we don't know if that web service is written in Visual Basic Java or C++ or whatever but what the tool will do is it will generate some Java classes that represent that web service it's going to look in the wisdom and for example it's going to work out that there's an operation supported called get employee by ID and it will generate a java method called get employee by ID and the beauty of that is here in the client we can then write Java code that goes off to the server and invokes the server operations regardless of what language that server was written in the only difficult thing is we do have to run that tool and it's a little bit of a fiddly command-line tool so I'm going to do this over on a caption well the name of the tool you need to run is called a WS import this is a standard tool part of the JDK so you already have it install you don't have to go away and download this and you're going to be running this tool from inside your projects directory so that means the directory where you have your SRC and your bin folder there's a couple of options here the - keep option and just to remind you what this tool is going to do it's going to Jen a set of Java classes representing the web service and at the same time it will also compile those Java files into binary classes now the - keep option here is simply telling the WS import tool that we would like to keep the original Java source files that's quite useful because you might want to inspect the contents of these Java files and you'll see in a moment that we will want to do exactly that the - verbose option is not that important that will just give us some verbose information on the console so if something goes wrong we might get a clue from that then the - s option here is pointing to the name of our source folder now I'm assuming you're using a standard Eclipse project so that folders going to be called dot forward slash SRC the dash D option is specifying the destination directory where we want these classes to be compiled and again if you're using the standard Eclipse project setup that's going to be dot /bin if you've used a different name such as classes then make sure you change it there and then after a space the important option here is the wisdom's URL so that's the URL that I currently have on my clipboard on previous courses such as our Java EE course where we also use Jet's WS I did indeed do this through the command line but that's really quite tedious and awkward and not platform-independent and really we can do it through eclipse of course I don't know how familiar you are with this process but the icon here which is supposed to be a play button and a small toolbox we need a sharp eyes to tell that but that's what it's supposed to be allows you to run external tools in Eclipse so if you drop that icon down using the little arrow to the right of the icon you can select the external tools configuration option select the program am item from here and then this isn't particularly clear the icon here is the new launch configuration button click that and we can point this dialog box at that WS import tool the name just needs to be a label that we can remember we will keep returning to this so I suggest WS import and for location we're going to need to browse the file system this step will vary depending on whether you're on Windows Linux or Macintosh so I'll assume you know where to find your Java development kits on Windows by default it generally installs itself on the C Drive under the Program Files folder and then Java and then you're looking for any of the JDK folders I have three separate versions of Java installed on my computer I'm going to choose the jdk 1.7 version purely because that's the one I'm using for my development I don't think it really matters to be honest and then in the bin folder it's going to be right down the bottom near the bottom the WS import ax will click on open for the working directory I'm not sure if we need this or not but this is just its root directory that it's working in actually that needs to be the project folder so just to be sure we'll add that in by clicking on browse workspace and then select your project folder which is the test client folder in the arguments box we now just need the command-line arguments that you saw on the previous caption which was - Keith - verbose I'm sure that that one's important but anyway then - s dot forward slash SRC I don't if we need the other forward slash but I'll put it in anyway and then - D for destination dot forward - been followed by another forward slash and then the last parameter is going to be the wisdoms URL well I happen to still have that on my clipboard so I'll paste that in there so the great thing about this is now this is going to be remembered by eclipse and when and if we need to rerun this tool we'll be able to find it from the tools icon but I can now click run the output from the tool will appear in the console and this is the kind of output you're looking for of course you don't want to see anything like a stack trace you want to see generating code followed by quite a few Java files and then compiling code and then no errors after the Java Klein if that has worked then the WS import tool has now generated some Java code for us and you'll be able to find the Java code if you click on your project and select refresh you should now see a new package under your SRC folder now you might be wondering how its kind of works out the package name given that remember the server code might not be written in Java but it's actually using the namespaces from the wisdom now if you don't know what I'm talking about there it doesn't really matter and we'll certainly be looking in more detail at that in a later chapter but for now it's generated a package and if we have a look in there there are a series of Java classes now want to make a really important point and I think I've said this several times so I'm sorry if I'm boring you but it bears repeating although you know and I know that the server here today has been written in Java it could have been written in any language it could have been Visual Basic for example but even so the WS import tool is able to generate for example a class called employee Java and it's done that by inspecting the XML inside the whistle it has in no way looked at the code on the server so this class is purely for the use of the clients and therefore if the server code had been written in Visual Basic we would still be seeing a Java class here on the client side now this is a generator Java class so we don't necessarily need to look at what's inside it but the crucial thing is that it's got an ID and a name and to make it usable there's a get ID asset ID the get name and a set name and I want to and I keep saying this this has not been generated by the WS in - looking at the java class on the server that's why there's no constructor in here and crucially there's no two-stringed method in here that's just on the server version of this class so the point of this class is that's going to enable us to manipulate employees here on the clients I think perhaps more importantly we have two classes here representing the actual service itself we have a generated class called employee service imple and we have a generated class called this is a bit strange because it's been generated it's called employee service in pull and it has the word service added to the end that's a little bit unfortunate that's just the generation here we call our class service but it adds another service on the end now the purpose of these two classes is to give us access to the operations defined on the web service and this class effectively goes behind the scenes and does all of the XML and it does the actual HTTP call to the server it's just actually using these classes is a little bit complicated and I always find it a little bit of a hoop jumping exercise now in fact the really important class for our purposes is this one here called employee service in pull that's a generator class that contains Java methods for each of the operations available on the web service if I have a look in there it's not a very pretty file to look at but the key thing is they're buried under a great big pile of annotations there is a method there called get employee by ID now that's the method we want to be able to call but you'll notice that this is an interface it's not actually a class now we're going to need to get hold of a concrete implementation of that class and the way we do that is by using this second class the one ending in service if you want to think of that as a factory class then it basically is a factory that's going to give us an instance of our service now it's a bit of a pain to work with it but what you do is you create a new instance of this Factory so it's the one with the ugliest name basically that we create an instance of and I think we should be able to import that I just did ctrl shift and oh there and if you immediately hit a dots after calling the new you should find a method in there called get employee service simple ports now you can remember that term port from before you can just think of port as being the web service itself you'll notice crucially that the return type of this object is called employee service input if we call that method then that's going to return so I'll go to the left hand side that's going to return an instance of employee service import and I'm going to call that web service then you're really home and dried because now on this web service if I spell it correctly and hit the dots you'll now find every single operation that's contained on the server's web service now we only have one of course this usual boring Java object methods but the one method that stands out there is get employee by ID I can pass in an ID now remember we're not actually using the ID on our service implementation but we could have been and we know that it's going to return an employee so the purpose of this that will need to be imported the purpose of this is to make us as the client programmer think that we're calling regular Java objects but actually what's going to happen when we call this web service object is it's going to go and do a network call and it will also convert the data that we've got here into XML message and when the service responds and returns an XML message back it will convert it back into this plain regular java class called employee it's actually quite slick so to test that that's works I can now do a system dot out dot print line now we don't cover to string on this remember this is the client employee and we've got a choice really we could go into the generated employee class and a dinner to string method but that's a little bit dangerous because we're going to be regenerating these classes every time the web service changes now you can do that if you're careful with your versioning of your classes but I'm just going to go in and do a get name on that object so I'm going to emphasize just one last time that what's happening here is that this is going to do a network call and we'll be accessing our server let's see if it works a right click oh by the way before I run you will need to make sure that your exporter from your server is still running otherwise this won't work what do we run as java application and we have a response I don't know if you did the previous chapter where we we have this situation but I just maximized the console we've now effectively got two consoles on top of each other we're seeing here the console for the client it's hard to tell that this is the clients console but we know because it's the one that's actually outputting the results if I click on the button here and drop that down you can see all of the current live consoles console 2 is the one that we're looking at and it's currently terminated but its console one I click on that that's the exporter that's the server and it's still running and you could see there was no output there just to prove that this really was making a network call to a separate java virtual machine if I kill the exporter and then try to run this client again we are going to see an exception and well you can pick through that and you'll see that the connection was refused it's a Java networking connection exception exactly what we'd expect to see if the service is down for some reason so the purpose of this demonstration project was just to give you the basic flavor for how jax-ws works this chapter was designed for anyone who hasn't seen the standard java library of jax-ws because spring is going to make heavy use of it I hope the overview of soap wasn't too basic for you I did oversimplify but don't worry we will go deeper later on especially on OS doors these are a bit of a nightmare to read although we can rely on auto generation for at least a while eventually you might need to write your own that will be coming later I really like the jax-ws library it makes writing and calling a soap web service simple perhaps a bit too simple as we'll see later on but as a start point it's pretty slick spring makes have use of jax-ws to support soap so in the next chapter we'll find out where spring fits into all of this
Info
Channel: undefined
Views: 85,608
Rating: 4.9191437 out of 5
Keywords: Java API For XML Web Services, SOAP (Internet Protocol), Web Service (Software Genre), Software (Industry)
Id: fE1pVSiXNkU
Channel Id: undefined
Length: 54min 30sec (3270 seconds)
Published: Thu Dec 11 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.