Join Patrick Royal for an in-depth discussion in this video Exploring WSDL, part of Java EE: Web Services.
In this video, we're going to look at some available options to customize the auto-generated WSDL file. We're going to use code from chapter one, video four, so if you skipped chapter one, you can download the code from the exercise files. As we saw in the previous video, the WSDL file is relatively easy to understand, but quite difficult to actually write. It's one thing to be able to look at a generated WSDL file and understand what each part does, and another entirely to be able to start with blank text document and write a WSDL file from scratch.
So, let's not do that. Instead, if I wanted to customize a WSDL file, I would first write a simple throwaway method and let GlassFish automatically generate a WSDL file for me. This takes care of the complicated definitions, URLs, port numbers, and so on. Then, I can customize the WSDL file by only changing the parts that aren't already what I want. Looking back at the code for the web service that was created in chapter one, there are a number of different things that can be customized.
First, let's customize the main WebService annotation. Looking back at the code for the web service the, that we created initially, there are a number of different things that can be customized. First, let's customize the main WebService annotation. If you put an open parenthesis after the annotation and hit Ctrl+space, or Cmd+space on a Mac, Eclipse will display a list of modifications that you can make. Most of the commands that appear deal with naming. From these commands, you can change the name of the WebService itself, the port, the service URL, or the package containing the service.
As an example, let's change the name of the WebService. The syntax here is name equals quotation marks and then the new name. Now, save, republish to server, and then open up your WDSL file again, or refresh it if it's already open, so that we can see the changes. Under portType, the name should be changed to the new name that you specified. The URLs' import type have also been changed accordingly.
Changing the other names works in the same way. The port name field changes the name of the port displayed under the service tag. The target name space field changes the name of the package, which is displayed in the auto-generated URLs in the port type tag. The service name field, changes the name of the service itself. Note, however, that changing the service name also changes the URL, so you'll have to manually update that in your browser, or else you'll get a' 404 error.
Then in the URL, look for a text string equal to your current name, which in my case is HelloWorld service, and simply replace it with your new name. The other customizable element is the entries for each individual method. WebMethods, which are methods run as part of web services, can be modified in several WebService specific ways. By default, if the class is labeled as WebService, Java will automatically consider all methods within the class to be WebMethods. But, in order to customize it, we will need to make the annotation explicit.
This is done in exactly the same way as for the class. Simply go before the method you want to change and type in @WebMethod. Once you've done this, you'll have to hover your mouse over the WebMethod annotation and choose to import javax.jws.WebMethod. There are three fields that can be used to customize a WebMethod. First, we can set an action. An action is simply a plain-text string that will be interpreted by the WSDL as an action within the operation.
This depends primarily on what your client wants, and how they intend to use your WebService. And, we'll get more into how actions are interpreted within SOAP in a later video. The second operation allows you to customize the operation name, which is just operationName equals, and then you put in a name. I'm going to minimize the outline to make it easier to see the full line here. This simply changes how the method is displayed, and it can be used to differentiate between the backend and frontend depictions of the method.
It's often useful to have a more verbose description of the method in your WSDL, but a simpler description for your own use while coding. Finally, the exclude option, which doesn't take a string, is simply used to designate certain methods not to be displayed in the WSDL. You'll probably use this a lot, especially for more complicated web services, because there may be many methods in the class, but only a few are meant to be accessible through the WebService.
- What are Java EE web services?
- Creating a simple service
- Building, packaging, and deploying your service
- Understanding the basic syntax
- Working with WSDL (Web Service Definition Language)
- Exploring SOAP (Simple Object Access Protocol)
- Exploring the syntax and design of REST (Representation State Transfer)
- Programming a web service in Java EE
- Debugging and optimizing your code