Working with NetBeans
In general, web services have two flavors: RESTful and (SOAP, WSDL). RESTful are supposed to be easier to consume. WSDL are more robust.
http://netbeans.org/kb/docs/websvc/intro-ws.html
There is an implementation named Axis, based on SOAP, also has services for RESTful.
http://netbeans.org/kb/docs/websvc/gs-axis.html
Creating Axis2 Web Service in NetBeans
I have created a web service using Axis2 and have accessed the service through a browser. It returns an XML file. http://netbeans.org/kb/docs/websvc/gs-axis.html
Creating Files to Access an Existing Web Service
I will now follow the tutorial to create a JAX-WS client that can access the service.
http://netbeans.org/kb/docs/websvc/client.html
First, I created the files to access an existing web service.
- Create web application
- New.Web Services.Web Service Client
- Specify URL of WSDL file: http://ws.cdyne.com/SpellChecker/check.asmx?wsdl
- Leave package blank, so it will be read friom WSDL
- Select JAX-WS as the client style.
- Many source files are generated.
- A local copy of the WSDL is generated.
Second, I created a client to access it.
- Create web application.
- Create user interface in JSP.
- Create servlet.
- Drag service component into servlet. The tutorial is wrong about the code that is inserted: a method is inserted. Call the method to run the service.
- Modify the servlet to call the service and update the UI.
The above approach uses NetBeans and Metro.
Working with Eclipse
I will now try to use Eclipse and Apache CXF.
Update Eclipse so it can use Tomcat to Run Servlets
First, add ability to run servlet in Eclipse.
- Download Eclipse with Java EE.
- Add extensions for JAX-WS from
http://wiki.eclipse.org/images/b/bf/Jaxws-1.0.0-201003201732.psf - Add them to Eclipse with File->Import->Team->Team Project File. The username is anonymous and the password is an email address.
- Create a Dynamic Web Application. Create a New Runtime (Server) for Tomcat. Select Create local server, to save a step later.
- Create servlet from New -> Servlet.
- Works perfectly.
- After I had to restore my computer, the eclipse installation was no longer working. Tomcat was not recognized. I switched workspaces (File->Switch Workspace) and reinstalled the Tomcat plugin. It is working again, but there are still some red Xs for some packages.
Second, download Apache CXF.
- http://www.apache.org/dyn/closer.cgi?path=/cxf/2.3.3/apache-cxf-2.3.3.zip
- Download Apache Ant: http://ant.apache.org/bindownload.cgi
- Follow video at:
http://repo.fusesource.com/videos/service-creation/Java2WSClientServer.wmv - The video is outdated. Click Add to enter the location of CXF. Be sure to check the new entry.
- In Eclipse: Window.Preferences.Web Services
- ...CXF Preferences; browse to install dir, select installation, export at runtime.
- ...JAX-WS; enable annotations; do not enable validation
- ..Spring Config; use Spring
- Create web app; select configuration as CXF; my install does not have springframework list, but is shown in video. Video shows
for runtime. To select CXF, I had to select Tomcat. - The Libraries are different from the video: jar files are in CXF, not Web App.
- Add converter for celsius and farenheit
- Make into web service: video shows Button Up POJO. That is not in my option list.
Creating Axis2 Web Service in Eclipse
Second attempt at Eclipse. New Tutorial:
http://www.softwareagility.gr/index.php?q=node/28
- Install Apache Axis2
- Create Dynamic web app; set configuration to include Axis2; define service class.
- Right-click service class and select Web Services.Create Web Service; set runtime to Axis2
- Run service; default Axis2 page appears; view service methods
- In Eclipse: Run.Launch Web Service Exporer; enter URL for WSDL; run methods
Creating Client to Access Web Service using Eclipse
Now, to create a client in Eclipse:
http://www.eclipse.org/webtools/community/tutorials/BottomUpAxis2WebService/bu_tutorial.html
- Tutorial creates another web service, like above.
- Tutorial then goes on to explain how to create client in separate app. In the first part, there was a check box for creating the client files. They were created in a separate app: same name as before, appended with Client; however, it does not have all the files as explained in the tutorial.
- Only one stub is created, not three (Http, Soap11, Soap12) as in tutorial.
- Imported the code to the client. Cannot be accessed in Tomcat.
- Try creating test case. They don't work either.
- I was planning to redo it all, when I noticed the stubs I had created inadvertently, when I first created the service. I decided to delete these. When I did, I received a lot of errors, so I undeleted them. At that moment, I noticed the return type on the celsiusFarenheit method. It was not a float; this was good, because that was a problem I was having with the others.
- I copied the client code from the web again and modified it with ClientStub.
- It worked!
- Solution: use the ConverterStub class, not any of the ones that have Soap in the name.
- Update on solution: it seems that you must create a completely new app for the client, do not import the stubs into a current app. The correct stubs were only created when creating a new client app.
No comments:
Post a Comment