building a simple web service with cxf
DESCRIPTION
This tutorial with demo how to build a simple web service with Java...TRANSCRIPT
![Page 2: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/2.jpg)
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.
![Page 3: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/3.jpg)
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.
![Page 4: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/4.jpg)
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.
![Page 5: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/5.jpg)
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
![Page 6: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/6.jpg)
Executing…
Fig.8 Starting STS.
Now, choose your workspace and click OK.
Fig.9 Choosing workspace.
![Page 7: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/7.jpg)
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
![Page 8: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/8.jpg)
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.
![Page 9: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/9.jpg)
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.
![Page 10: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/10.jpg)
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.
![Page 11: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/11.jpg)
After choosing the version, select the path of your Tomcat.
Fig.15 Tomcat installation directory.
![Page 12: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/12.jpg)
It’s done, click Next.
Fig.16
![Page 13: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/13.jpg)
Changing the Default output folder to WebContent/WEB-INF/classes.
Fig.17 Default output folder.
![Page 14: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/14.jpg)
Last step, select “Generate web.xml deployment descriptor”.
Fig.18 Generate web.xml deployment descriptor.
![Page 15: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/15.jpg)
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.
![Page 16: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/16.jpg)
Choose General -> File System.
Fig.21 Choose “File System”.
![Page 17: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/17.jpg)
Navigating to your CXF folder, and select all jar files in lib.
Fig.22 Jar files in lib.
![Page 18: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/18.jpg)
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.
![Page 19: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/19.jpg)
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.
![Page 20: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/20.jpg)
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
![Page 21: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/21.jpg)
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.
![Page 22: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/22.jpg)
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.
![Page 23: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/23.jpg)
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.
![Page 24: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/24.jpg)
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.
![Page 25: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/25.jpg)
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.
![Page 26: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/26.jpg)
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.
![Page 27: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/27.jpg)
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.
![Page 28: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/28.jpg)
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.
![Page 29: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/29.jpg)
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.
![Page 30: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/30.jpg)
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.
![Page 31: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/31.jpg)
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.
![Page 32: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/32.jpg)
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.
![Page 33: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/33.jpg)
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.
![Page 34: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/34.jpg)
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.
![Page 35: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/35.jpg)
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
![Page 36: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/36.jpg)
Now, execute your main class.
Fig.48 Debig as Java application.
![Page 37: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/37.jpg)
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.
![Page 38: Building A Simple Web Service With CXF](https://reader033.vdocuments.us/reader033/viewer/2022052619/5550c316b4c905ff618b5486/html5/thumbnails/38.jpg)
If you can see the output as below, congratulation, you’ve finished a
simple web application.
Fig.50utput.