CXF MTOM DATA BINDING
Upload attachment (document) using CXF ( MTOM ) Spring and Tomcat
Incase you have any difficulty in setup please contact us.Let us now create a resume upload web service using Apache CXF, Spring and Tomcat and consume it. The web service will be used to upload an attachment [resume]. The scenario considered here is a candidate uploading his/her resume and the resume [word document] is retrieved and stored on the server.
The following files are involved in the demo of the tutorial. Download the files (zip format)
Resume.java This DTO (value object) represents the resume of a candidate.
ResumeUploadService.java This interface has uploadResume method which accepts a Resume.
ResumeUploadServiceImpl.java Implementation class for ResumeUploadService.java
Client.java Code which consumes the web service.
cxf.xml Informs Spring [which is referenced from CXF] about the related Java classes for a given web service.
Before getting into the details of the above files let us setup a web application in Tomcat 5.5 or Tomcat 6 to deploy the resume upload web service.
Step 1: Go to TOMCAT_HOME\webapps folder [In my machine it is C:\tomcat\webapps]
Step 2: Create a new folder and name it as resumeupload
Step 3: Create a folder by the name WEB-INF under resumeupload
Step 4: Create two folders classes and lib under WEB-INF.
Step 5: Download web.xml [right click and save link as]and save it under WEB-INF folder.Now your folder structure should look as in the screen shot below.
Step 6: Download CXF in zip format and unzip it into any folder. Copy the jar files under CXF lib folder to the lib folder under Tomcat (created in Step 4). Though all jar files are not necessary, we will make use of them in our future CXF tutorials.
Step 7: Now setup a Java project in Eclipse [with JDK 1.5 plus ] so that we can populate the classes folder created in Step 4. Download and copy the files under source directory of the Java project as in the screen shot below.
Step 8: To resolve compilation errors add the jars under lib folder of CXF [downloaded in Step 6] to Java Build Path/Libraries of the Eclipse project.
Step 9: Build the Eclipse project and copy the classes [the cxf.xml also will be placed by eclipse along with the classes if you have selected separate folders for source and classes when setting up the eclipse Java project] under the classes folder created in Step 4. [You may exclude Client.class as it is not required under Tomcat]
Step 10: Restart the Tomcat. The deployment of the web service on Tomcat server is complete. Before we invoke the client side code [which is a breeze] let us understand the code in the files involved.
Resume.java
Resume.java consists of getters and setters for name of the candidate, resume type [if it is doc or pdf], and the resume [attachment].
ResumeUploadService.java
ResumeUploadService.java declares the uploadResume method with the annotations. The @WebParam annotation though optional is required for a readable WSDL file [if not specified the parameter names will be as arg0, arg1 and not be meaningful....].
ResumeUploadServiceImpl.java
ResumeUploadServiceImpl.java is the server side implementation of the web service.
It retrieves the InputStream from the DataHandler and stores the resume in C drive with the c candidateName.extensionType. The related annotations are also provided.
cxf.xml
cxf.xml has the configuration which will be loaded by Spring and used by CXF about the web service. We have mentioned jaxws:properties which enables mtom capability of CXF where in the resume is sent as an attachment giving better performance [by avoiding base 64 encoding].
Client.java
Client.java populates values for candidate name, file attachment type, and the resume [which is path to a file] and invokes the web service.
web.xml
Finally about the web.xml which we downloaded and placed under Tomcat WEB-INF folder. This configures CXF Servlet as well as the location of cxf.xml which is to be loaded by Spring.
Now we will go back to our Eclipse project and run Client.java as a Java application. [Please change the values of server port [8080 in this example], resume location, resume file type and candidate name in Client.java as required.]. You should be able to see the file getting stored under the location specified in ResumeUploadServiceImpl.java [in this example C drive] with the candidateName.extensionType.
This tutorial is suitable only for small attachments. To learn how to upload large attachments please refer Upload big attachment (document) using CXF ( MTOM )
Here let us tweak the code provided in the above tutorial to enable MTOM streaming capability at the client [client to the web service].
The modified section in Client.java is as below.
Map
props.put("mtom-enabled", Boolean.TRUE);
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setProperties(props);
factory.getInInterceptors().add(new LoggingInInterceptor());
Comments
I am the author of the tutorial at TheaSolutions. I would be satisfied if you post a link to the tutorial rather than copying the content in your blog.
Or atleast mention it is from Theasolutions.com
The link is http://www.theasolutions.com/tutorials/cxfuploadattachment.jsp
link http://www.theasolutions.com/tutorials/cxfuploadattachment.jsp is not working can you give me link so that i can download source.