2. This is a simple tutorial for beginner who wants to learn how to develop
some webservices in Java.
Before we start, you should prepare some tools first :
1. Java SE
2. Spring Tool Suite(STS)
3. Apache CXF
4. Tomcat
OK, letâs set up the development environment. I assumed that youâve
already installed Java in your OS, so weâll start from installing STS.
Go to Spring source community, and scroll down to the bottom of
homepage.
Fig.1 Spring source community.
3. At the bottom of page, choose âGet Tool Kit(STS)â.
Fig.2 Choose âGet Tool Kit(STS)â.
As Fig.3, go to the download page.
Fig.3 Click the download buttom.
4. Itâs up to you to provide your private information or not, you can also
click âtake me to the download pageâ.
Fig.4 You can skip it.
At the download page, choose the product you want, if youâre using
windows, itâs better to choose installer.exe file.
Fig.5 Choose your STS.
5. After installing your STS, there are four folders under springsource folder.
Fig.6 springsource folder
Click âsts-x.x.x.RELEASEâ, then execute âSTS.exeâ.
Fig.7 STS.exe
6. ExecutingâŚ
Fig.8 Starting STS.
Now, choose your workspace and click OK.
Fig.9 Choosing workspace.
7. Great, youâve finish setting your IDE, now itâs time to obtain the
framework(CXF) and container(Tomcat) you need.
Go to Apache CXF and choose the version you want.
Fig.10 Apache CXF
In Apache Tomcat, you can choose Core because itâs enough for us.
Fig.11 Apache Tomcat
8. Letâs back to STS, itâs time to build your first web application in Java.
Choose File->New->Dynamic Web Project.
Fig.12 Dynamic Web Project.
9. Before naming your project, please take a look at "Target runtime"
option, if the option is not an Apache Tomcat, click âNew Runtimeâ.
Fig.13 Check Target runtime.
10. Then, youâll see the following list, just choose the Apache Tomcat which
is match to your download version(My Tomcat is v7.0âŚ).
Fig.14 Choose the Tomcat you want.
11. After choosing the version, select the path of your Tomcat.
Fig.15 Tomcat installation directory.
15. Now, youâve build a project as below:
Fig.19 Your project.
Letâs import our CXF resource now, right click on the folder
WebContent/WEB-INF/lib and choose âImportâ.
Fig.20 Import.
17. Navigating to your CXF folder, and select all jar files in lib.
Fig.22 Jar files in lib.
18. Youâve finished all the settings in this project, itâs time to write some
code now. First, right click on your project and then new an interface.
Fig.23 New an interface.
Itâs important to define packages in your project, a good classification of
packages can help you to manage your code effectively.
Fig.24 Define package.
19. We assume that our web service is an greeting system, it will say hello to
our ccustomer. According to this demand, we can define our service
interface as below:
Fig.25 Greeting system.
But, itâs not enough. Because CXF can not realize that this interface is for
web services. So we need to add some annotaion⌠.
Fig.26 Add some annotaion.
Explanation:
ďŹ @WebService: It means that the interface is a web service.
ďŹ @WebMethod: It means that this is a method in web service.
ďŹ @WebParam: The parameter in method, you can decide the
name of input parameter by name attribute.
20. Now, you've had an service interface, so letâs create a class to implement
this service:
Fig.27 The implementation of interface.
With an implementation, now we can create a web.xml file under
WebContent/WEB-INF.
Fig.28 web.xml
21. Explanation:
ďŹ <context-param>:
Specify the corresponding configuration of spring.
ďŹ <param-value>:
Specify the location of configuration document.
ďŹ <url-pattern>:
The pattern of url when the service is called by someone.
Next, create service-beans.xml under WebContent/WEB-INF:
Fig.29 Itâs an XML file.
22. Coding followed Fig.29:
Fig.30 service-beans.xml
Explanation:
ďŹ <jaxws:endpoint>:
It defines a web service, the attribute âimplementorâ means
that the actual class which will handle the web service and its
value is mapping to the âidâ of <bean> , finally, the concrete
class will be specified in âclassâ of <bean>. And âaddressâ is the
pattern shown in URL.
23. We've almost finished our server-side application. But it still have a few
steps to do. We need to deploy our application to Tomcat.
Right click on your project and select Export->WAR file.
Fig.31 Packaging your project to WAR file.
24. The WAR file should be put under the folder âwebappsâ of your Tomcat
directory. Also, you need to check âTarget runtimeâ here to ensure itâs a
Tomcat server and donât forget to select optimization option.
Fig.32 WAR Export.
25. Then, move to your Tomcat directory, you can see that there is a .bat file
called startup.bat under the bin folder. This batch can startup your
Tomcat server, so, just click it.
Fig.33 Startup your Tomcat.
26. The figure below is the startup snapshot of Tomcat server. To ensure
your Tomcat has startup successfully, you can look at the bottom to see
whether there is a message âServer startup in xxx msâ or not.
Fig.34 Startup Server.
27. After startup the server, letâs see our web service on internet. Here, you
can use the browser you like, and you can also use the built-in browser in
STS, with this built-in function, you should switch your perspective to
âJava EEâ, just like below:
Fig.35 Built-in browser.
To see our web service, enter the following URL:
âhttp://localhost:8080/MyCXFâ.
Fig.36 Our web service.
Here, youâll see âEndpoint addressâ, this is the location of specific service,
because we only built one service called âgreetingHelloâ so there is only
one endpoint address.
28. If you click the WSDL hyperlink below Endpoint address, youâll see the
WSDL of greetingHello like Fig.36 .
Fig.37 WSDL file.
OK, itâs the end of establishing our server-side application. Now itâs time
to build our client application.
29. Weâve already established our server-side application, now, we can use
CXF to help us to generate a simple client application called stub. Itâs
very convenient because we donât need to program ourselves.
First, open the CXF package youâve downloaded. In the bin folder, you
will see lots of batch files named with âwsdl2XXXâ, these kind of batch
file can help us to generate stub according to specific services.
Fig.38 wsdl2XXX batch.
30. With these tools, we can generate client-side application automatically
by the following command:
wsdl2XXX âp package_name_of_generated_code
âd path_of_generated_code wsdl_address
Fig.39 Using wsdl2java.
In Fig.38, we use âwsdl2javaâ to generate our client-side application.
31. Moving to the directory you specified and you can see that in the
package you defined has generated several java files. These files are your
client-side codes.
Fig.40 Generated client code.
32. Now, we can start to build our client-side project, back to STS, choose
âJava Projectâ as your project type.
Fig.41 Choosing Java Project.
Fig.42 Create a Java Project.
33. The first thing we need to do is to import the generated client-side code,
right click on your project name, choose import option, and then select
the generated package.
After importing the package, we need to include some CXF libraries.
Choose âBuild Pathâ -> âConfigure Build Pathâ.
Fig.43 Configure Build Path.
34. Click tag âLibrariesâ, choose âAdd External JARsâ.
Fig.44 Libraris.
Select all files in the CXF lib folder.
Fig.45 Select all files in lib.
35. Create a class include main function as the entrance of the application.
Fig.46 Main function.
Finally, create a xml called âclient-beans.xmlâ in the current package as
below:
Fig.47 Client-beans.xml
37. Here, if you encounter some errors like Fig.48, just comment those
constructor. Because we have not implemented those constructors with
several parameters.
Fig.49 Comment these constructors.
38. If you can see the output as below, congratulation, youâve finished a
simple web application.
Fig.50utput.