Generating Java classes from WSDL files
Ready to discover a way to generate usable Java classes from WSDL files and check how they are structured? That is precisely where this blog post will get you.
Finishing the last line, you’ll be equipped with all the necessary schema files and the ready-to-use service.
Sounds promising? Only if you know what WSDL files are, so let’s start with defining our key phrase.
One way of communication between applications is SOAP protocol based on XML documents. WSDL files come in handy when describing the application's web services using SOAP without writing documentation. You can use them to generate the code ready for API testing and the object structure that can be mapped from incoming responses and is necessary for creating requests.
This scenario comes straight from our recent project — we’ve got WSDL files from one of the applications we need to reach as a part of the instruction on communicating with them.
The description of each package in the service consisted of two files.
The .xsd file with a defined target namespace, names of data classes in the given package, and types and names of their fields, which describes schema.
The .wsdl file describing services, such as their name and all the methods with parameters and types returned.
Mind that it’s also possible to describe schema and service in one WSDL file instead of two separate ones.
HOW TO GENERATE THE CLASSES
Start with adding both WSDL files to your project.
No framework is necessary, but let’s assume it’s a Spring project.
The default directory for the files is src/main/resources. Then, you need the library to generate classes.
In our project, we decided to use cxf-codegen-plugin. It is possible to use this plugin with Gradle, but we chose Maven, so I will use it as an example.
The basic Maven dependency, together with the execution configuration, is quite simple:
And that’s it. Now, it’s possible to generate the classes only with this additional dependency in the project.
You can also customise how the classes are generated using the <wsdlOptions> tag in the configuration part.
What exactly can be customised?
Name of the generated service:
Name of the created package:
By default, files are generated in ${project.build.directory}/generated-sources/cxf/ticket. With the given configuration, it would be ${project.build.directory}/wsdl/service/ticket package.
Single WSDL files we don’t want to generate classes from:
The final step is running the mvn clean install command. When you get the build success, your classes are ready.
LET’S SEE HOW IT WORKS
Let’s generate classes from Ticket.wsdl and Ticket.xsd files to see it on a specific example. We can observe that they describe the TicketService and all the necessary schema classes.
Ticket.wsdl file:
Ticket.xsd file:
Below there’s a picture showing what the generated structure looks like:
According to the Ticket.xsd, two files were generated — a ticket class containing two fields with getters and setters and an enum class TicketType with defined values.
In the WSDL definition, it’s also possible to validate field values with @XmlElement annotation, e.g. making them required with the ‘required = true’ parameter, like by customerId and ticketType fields. This parameter is the default, and to change it, you would have to add a ‘minOccurs=”0”’ parameter in the <xs:element> tags defining these fields.
Based on Ticket.wsdl, you obtain an interface TicketInterface with one method. As specified, it gets a String parameter and returns a Ticket class instance.
The TicketService class contains the method returning TicketInterface, which allows us to use the getTicket() method and test the connection with the external API:
In the ticket package, there’s also the ObjectFactory class, with methods you can use for creating new instances of all the other generated types.
HOW TO USE GENERATED CLASSES
When the classes are ready, you can use them like any other class. If the project uses Spring, it may be useful to register a bean in a configuration class implementing BeanDefinitionRegistryPostProcessor, as below:
As you understand by now, using WSDL files allows you to test the connection quickly and save time you would otherwise use to prepare the model of the received response.
Why not give it a try?