Hello Guys!
I am running on
Java 7. I have just started off with web services. I have client and server code communicating (supposed to) through
SOAP.
Requirement is to pass csv file as attachment in SOAP response, but first step is to attach simple text file to SOAP response.
----------------------------------------------------------------------------------------------------------------------------------------
package com.balaji.ws;
import java.awt.Image;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.soap.SOAPMessage;
//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface ImageServer{
@WebMethod SOAPMessage downloadImageSOAP(
String string);
}
----------------------------------------------------------------------------------------------------------------------------------------
package com.balaji.ws;
import java.io.File;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.jws.WebService;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
//Service Implementation Bean
@WebService(endpointInterface = "com.balaji.ws.ImageServer")
public class ImageServerImpl implements ImageServer {
@Override
public SOAPMessage downloadImageSOAP(String string) {
SOAPMessage soapMessage = null;
try {
MessageFactory messageFactory = MessageFactory.newInstance();
soapMessage = messageFactory.createMessage();
File file = new File(
"C:\\Users\\balaji\\Desktop\\test.txt");
DataHandler dataHandler = new DataHandler(new FileDataSource(file));
AttachmentPart attachmentPart = soapMessage
.createAttachmentPart(dataHandler);
attachmentPart.setContentId("Image File");
attachmentPart.setMimeHeader("header1", "value1");
attachmentPart.setMimeHeader("header2", "value2");
soapMessage.addAttachmentPart(attachmentPart);
System.out.println(soapMessage.countAttachments());
} catch (SOAPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return soapMessage;
}
}
----------------------------------------------------------------------------------------------------------------------------------------
package com.balaji.endpoint;
import javax.xml.ws.Endpoint;
import com.balaji.ws.ImageServerImpl;
//Endpoint publisher
public class ImagePublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws/image", new ImageServerImpl());
System.out.println("Server is published!");
}
}
----------------------------------------------------------------------------------------------------------------------------------------
package com.balaji.client;
import java.awt.Image;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.soap.MTOMFeature;
import javax.xml.ws.soap.SOAPBinding;
import com.balaji.ws.ImageServer;
public class ImageClient{
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:9999/ws/image?wsdl");
QName qname = new QName("http://ws.balaji.com/", "ImageServerImplService");
Service service = Service.create(url, qname);
ImageServer imageServer = service.getPort(ImageServer.class);
/************
test download ***************/
SOAPMessage returnMessage = imageServer.downloadImageSOAP("test.txt");
System.out.println("imageServer.downloadImage() : Download Successful!");
}
}
----------------------------------------------------------------------------------------------------------------------------------------
The wsdl is shown properly in chrome browser when I enter
http://localhost:9999/ws/image?wsdl.
First I run the ImagePublisher.java and then run ImageClient.java.
I expected at least a ful SOAPMesage object but I get the following exception:
Exception in
thread "main" com.sun.xml.internal.ws.encoding.soap.DeserializationException: Failed to read a response: javax.xml.bind.UnmarshalException
- with linked exception:
[com.sun.istack.internal.SAXParseException2; lineNumber: 1; columnNumber: 169; Unable to create an instance of javax.xml.soap.SOAPMessage]
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(Unknown Source)
at com.sun.proxy.$Proxy8.downloadImageSOAP(Unknown Source)
at com.balaji.client.ImageClient.main(ImageClient.java:34)
Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[com.sun.istack.internal.SAXParseException2; lineNumber: 1; columnNumber: 169; Unable to create an instance of javax.xml.soap.SOAPMessage]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.BridgeImpl.unmarshal(Unknown Source)
at com.sun.xml.internal.bind.api.Bridge.unmarshal(Unknown Source)
at com.sun.xml.internal.ws.client.sei.ResponseBuilder$RpcLit$PartBuilder.readResponse(Unknown Source)
at com.sun.xml.internal.ws.client.sei.ResponseBuilder$RpcLit.readResponse(Unknown Source)
... 5 more
Caused by: com.sun.istack.internal.SAXParseException2; lineNumber: 1; columnNumber: 169; Unable to create an instance of javax.xml.soap.SOAPMessage
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ProxyLoader.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source)
... 10 more
Caused by: javax.xml.bind.UnmarshalException: Unable to create an instance of javax.xml.soap.SOAPMessage
- with linked exception:
[java.lang.InstantiationException]
... 20 more
Caused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.sun.xml.internal.bind.v2.ClassFactory.create0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance(Unknown Source)
... 18 more
----------------------------------------------------------------------------------------------------------------------------------------
Please let me know where I am going wrong! I am extremely desperate!
Thank you!