JAX-WS Binary Data Passing: using base64binary

Instead of passing binary data as an attachment to web services using either SOAP with Attachment or MTOM, in this blog, I use base64binary to pass in the SOAP body. For the MTOM (preferred way of sending binary data) see the next blog JAX-WS Binary Data passing: using MTOM.

Web service as follows:

package org.ojitha.wsex4;

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService(serviceName="ImageService")
public class ImageService {
	
	@WebMethod
	public Image getImage(String imgName) throws IOException{
		
		return ImageIO.read(new File(imgName));
	}
	
	public static void main(String[] args){
		Endpoint.publish("http://127.0.0.1:9876/image", new ImageService());
	}

}

The client program is,

package org.ojitha.wsex4.client;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class ImageClient {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		ImageService_Service service = new ImageService_Service();
		ImageService image = service.getImageServicePort();
		try {
			byte[] bytes =image.getImage("rainbow.jpg");
			InputStream in = new ByteArrayInputStream(bytes);
			BufferedImage buffferedImage = ImageIO.read(in);
			ImageIO.write(buffferedImage, "jpg", new File("test.jpg"));
		} catch (IOException_Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

if you trace the SOAP response, the image was sent in the body instead an attachment. Source code available to download.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python