Receiving the binary content using DataHandler

The File.java is a simple bean that has 2 properties a string and a DataHandler, the DataHandler is used to receive the file contents sent from the consumer of the web service.

  • Since the File.java is transfered between the consumer and the web service it needs to be serialized and sent in xml format. The @XmlType and @XmlMimeType("application/octet-stream") annotations define that. When sending binary content between a web service, we can make use of the DataHandler and the @XmlMimeType("application/octet-stream") annotation to receive that. cxf automatically takes care of creating the underlying transports and formats.
    package com.yosanai.tutorial.ws.hellowebservice;
    
    import javax.activation.DataHandler;
    import javax.xml.bind.annotation.XmlMimeType;
    import javax.xml.bind.annotation.XmlTransient;
    import javax.xml.bind.annotation.XmlType;
    
    /**
     * @author Saravana P Shanmugam
     * 
     */
    @XmlType
    public class File {
    
    	@XmlTransient
    	protected String fileName;
    
    	@XmlTransient
    	protected DataHandler fileContent;
    
    	/**
    	 * 
    	 */
    	public File() {
    	}
    
    	/**
    	 * @return the fileName
    	 */
    	public String getFileName() {
    		return fileName;
    	}
    
    	/**
    	 * @param fileName
    	 *            the fileName to set
    	 */
    	public void setFileName(String fileName) {
    		this.fileName = fileName;
    	}
    
    	/**
    	 * @return the fileContent
    	 */
    	@XmlMimeType("application/octet-stream")
    	public DataHandler getFileContent() {
    		return fileContent;
    	}
    
    	/**
    	 * @param fileContent
    	 *            the fileContent to set
    	 */
    	public void setFileContent(DataHandler fileContent) {
    		this.fileContent = fileContent;
    	}
    
    }
    

Who's online

There are currently 0 users and 1 guest online.

Who's new

  • Saravana Peruma...