Created
November 30, 2013 21:51
-
-
Save HimaiMinh/7725001 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Ivan's StringProcessor web service. | |
package stringProcessor; | |
import javax.jws.soap.SOAPBinding; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBElement; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Marshaller; | |
import javax.xml.bind.Unmarshaller; | |
import javax.xml.bind.util.JAXBSource; | |
import javax.xml.transform.Source; | |
import javax.xml.ws.Provider; | |
import javax.xml.ws.Service; | |
import javax.xml.ws.ServiceMode; | |
import javax.xml.ws.WebServiceProvider; | |
@WebServiceProvider( | |
wsdlLocation="StringProcessorService.wsdl", | |
portName="StringProcessorServicePort", | |
serviceName="StringProcessorService", | |
targetNamespace="http://www.ivan.com/stringprocessor") | |
@ServiceMode(value = Service.Mode.PAYLOAD) | |
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED, | |
style = SOAPBinding.Style.DOCUMENT, | |
use = SOAPBinding.Use.LITERAL) | |
public class StringProcessor implements Provider<Source> | |
{ | |
/* (non-Javadoc) | |
* @see javax.xml.ws.Provider#invoke(java.lang.Object) | |
*/ | |
public Source invoke(final Source inRequestMessage){ | |
Source theResponseSource = null; | |
try { | |
/* Prepare JAXB for marshaling and unmarshaling. */ | |
JAXBContext theJaxbContext = | |
JAXBContext.newInstance("stringProcessor"); | |
Unmarshaller theUnmarshaller = theJaxbContext.createUnmarshaller(); | |
Marshaller theMarshaller = theJaxbContext.createMarshaller(); | |
JAXBElement unMarshalled = (JAXBElement)theUnmarshaller.unmarshal(inRequestMessage); | |
final Class<?> operation = unMarshalled.getDeclaredType(); | |
ReverseStringReq theReq = null; | |
if(operation.isAssignableFrom(ReverseStringReq.class)){ | |
theReq = ((JAXBElement<ReverseStringReq>)unMarshalled).getValue(); | |
System.out.println("operation get class : "+operation.getClass().toString()); | |
System.out.println("Got a request to reverse the string: " + | |
theReq.getInString()); | |
} | |
/* | |
* Do the processing supplied by the operation - that is | |
* reverse the string. | |
*/ | |
String theResultString = | |
(new StringBuffer(theReq.getInString())).reverse().toString(); | |
System.out.println("Processed the result: " + theResultString); | |
/* Create the response message payload and set the result. */ | |
ObjectFactory theObjFactory = new ObjectFactory(); | |
ReverseStringResp theResponse = | |
theObjFactory.createReverseStringResp(); | |
theResponse.setReturn(theResultString); | |
/* | |
* Create a JAXB source that will marshal the supplied | |
* JAXB bean to XML when the response message is assembled. | |
*/ | |
theResponseSource = new JAXBSource(theJaxbContext, theResponse); | |
} | |
catch (JAXBException theException){ | |
theException.printStackTrace(); | |
} | |
return theResponseSource; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stringProcessor; | |
import javax.xml.bind.annotation.XmlAccessType; | |
import javax.xml.bind.annotation.XmlAccessorType; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
import javax.xml.bind.annotation.XmlType; | |
/** | |
* <p>Java class for reverseStringReq complex type. | |
* | |
* <p>The following schema fragment specifies the expected content contained within this class. | |
* | |
* <pre> | |
* <complexType name="reverseStringReq"> | |
* <complexContent> | |
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> | |
* <sequence> | |
* <element name="inString" type="{http://www.w3.org/2001/XMLSchema}string"/> | |
* </sequence> | |
* </restriction> | |
* </complexContent> | |
* </complexType> | |
* </pre> | |
* | |
* | |
*/ | |
@XmlRootElement | |
@XmlAccessorType(XmlAccessType.FIELD) | |
@XmlType(name = "reverseStringReq", propOrder = { | |
"inString" | |
}) | |
public class ReverseStringReq { | |
@XmlElement(required = true) | |
protected String inString; | |
/** | |
* Gets the value of the inString property. | |
* | |
* @return | |
* possible object is | |
* {@link String } | |
* | |
*/ | |
public String getInString() { | |
return inString; | |
} | |
/** | |
* Sets the value of the inString property. | |
* | |
* @param value | |
* allowed object is | |
* {@link String } | |
* | |
*/ | |
public void setInString(String value) { | |
this.inString = value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package client; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.Map; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBElement; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.namespace.QName; | |
import javax.xml.ws.Dispatch; | |
import javax.xml.ws.Service; | |
import javax.xml.ws.handler.MessageContext; | |
import javax.xml.ws.http.HTTPBinding; | |
import stringprocessor1client.ObjectFactory; | |
import stringprocessor1client.ReverseStringReq; | |
import stringprocessor1client.ReverseStringResp; | |
public class StringProcessor1Client { | |
//The endpoint location where GlassFish deploy the StringProcessorService http://localhost:8080/StringProcessor1/String | |
private final static String WSDL_LOCATION ="http://localhost:8080/StringProcessor1/StringProcessorService?wsdl"; | |
private final static String SERVICE_NAMESPACE ="http://www.ivan.com/stringprocessor"; | |
private final static String SERVICE_NAME = "StringProcessorService"; | |
private final static String SERVICE_PORT_NAME = "StringProcessorServicePort"; | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) throws MalformedURLException, JAXBException { | |
//Create a new port that is not in the WSDL | |
QName newportQname = new QName(SERVICE_NAMESPACE, "StringProcessorServicePort1"); | |
QName serviceQName = new QName(SERVICE_NAMESPACE,SERVICE_NAME); | |
URL url = new URL(WSDL_LOCATION); | |
Service service = Service.create(url, serviceQName); | |
service.addPort(newportQname, HTTPBinding.HTTP_BINDING,"http://localhost:8080/StringProcessor1/StringProcessorService"); | |
JAXBContext theJaxbContext = JAXBContext.newInstance("stringprocessor1client"); | |
Dispatch<Object> dispatch = service.createDispatch(newportQname, theJaxbContext, Service.Mode.PAYLOAD); | |
ObjectFactory theJaxbObjFactory = new ObjectFactory(); | |
ReverseStringReq request = new ReverseStringReq(); | |
request.setInString("Exam"); | |
JAXBElement<ReverseStringReq> reqElement = theJaxbObjFactory.createReverseStringReq(request); | |
JAXBElement<ReverseStringResp> response = (JAXBElement<ReverseStringResp>) dispatch.invoke(reqElement); | |
System.out.println(" New response : "+ response.getValue().getReturn()); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Error Message: | |
Exception in thread "main" javax.xml.ws.WebServiceException: javax.xml.bind.UnmarshalException | |
- with linked exception: | |
[javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are <{http://www.ivan.com/stringprocessor}reverseStringReq>,<{http://www.ivan.com/stringprocessor}reverseStringResp>] | |
at com.sun.xml.internal.ws.client.dispatch.JAXBDispatch.toReturnValue(JAXBDispatch.java:82) | |
at com.sun.xml.internal.ws.client.dispatch.DispatchImpl.doInvoke(DispatchImpl.java:191) | |
at com.sun.xml.internal.ws.client.dispatch.DispatchImpl.invoke(DispatchImpl.java:195) | |
at client.StringProcessor1Client.main(StringProcessor1Client.java:44) | |
Caused by: javax.xml.bind.UnmarshalException | |
- with linked exception: | |
[javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are <{http://www.ivan.com/stringprocessor}reverseStringReq>,<{http://www.ivan.com/stringprocessor}reverseStringResp>] | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:414) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:351) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:321) | |
at com.sun.xml.internal.ws.message.stream.StreamMessage.readPayloadAsJAXB(StreamMessage.java:236) | |
at com.sun.xml.internal.ws.message.stream.PayloadStreamReaderMessage.readPayloadAsJAXB(PayloadStreamReaderMessage.java:102) | |
at com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlContent.readPayloadAsJAXB(XMLMessage.java:327) | |
at com.sun.xml.internal.ws.client.dispatch.JAXBDispatch.toReturnValue(JAXBDispatch.java:74) | |
... 3 more | |
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are <{http://www.ivan.com/stringprocessor}reverseStringReq>,<{http://www.ivan.com/stringprocessor}reverseStringResp> | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:631) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:236) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:231) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1038) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:467) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:448) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:231) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:165) | |
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:349) | |
... 8 more | |
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are <{http://www.ivan.com/stringprocessor}reverseStringReq>,<{http://www.ivan.com/stringprocessor}reverseStringResp> | |
... 19 more | |
Java Result: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='UTF-8'?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Metro/2.2.0-1 (tags/2.2.0u1-7139; 2012-06-02T10:55:19+0000) JAXWS-RI/2.2.6-2 JAXWS/2.2 svn-revision#unknown. --><definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://www.ivan.com/stringprocessor" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://www.ivan.com/stringprocessor" name="StringProcessorService"> | |
<types> | |
<xsd:schema> | |
<xsd:import namespace="http://www.ivan.com/stringprocessor" schemaLocation="http://localhost:8080/StringProcessor1/StringProcessorService?xsd=1"/> | |
</xsd:schema> | |
</types> | |
<message name="reverseStringRequest"> | |
<part name="parameters" element="tns:reverseStringReq"/> | |
</message> | |
<message name="reverseStringResponse"> | |
<part name="result" element="tns:reverseStringResp"/> | |
</message> | |
<portType name="StringProcessorPort"> | |
<operation name="reverseString"> | |
<input message="tns:reverseStringRequest"/> | |
<output message="tns:reverseStringResponse"/> | |
</operation> | |
</portType> | |
<binding name="StringProcessorPortBinding" type="tns:StringProcessorPort"> | |
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> | |
<operation name="reverseString"> | |
<soap:operation soapAction="urn:ReverseString"/> | |
<input> | |
<soap:body use="literal"/> | |
</input> | |
<output> | |
<soap:body use="literal"/> | |
</output> | |
</operation> | |
</binding> | |
<service name="StringProcessorService"> | |
<port name="StringProcessorServicePort" binding="tns:StringProcessorPortBinding"> | |
<soap:address location="http://localhost:8080/StringProcessor1/StringProcessorService"/> | |
</port> | |
</service> | |
</definitions> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='UTF-8'?> | |
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Metro/2.2.0-1 (tags/2.2.0u1-7139; 2012-06-02T10:55:19+0000) JAXWS-RI/2.2.6-2 JAXWS/2.2 svn-revision#unknown. --> | |
<xs:schema xmlns:tns="http://www.ivan.com/stringprocessor" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://www.ivan.com/stringprocessor"> | |
<xs:element name="reverseStringReq" type="tns:reverseStringReq"/> | |
<xs:element name="reverseStringResp" type="tns:reverseStringResp"/> | |
<xs:complexType name="reverseStringReq"> | |
<xs:sequence> | |
<xs:element name="inString" type="xs:string"/> | |
</xs:sequence> | |
</xs:complexType> | |
<xs:complexType name="reverseStringResp"> | |
<xs:sequence> | |
<xs:element name="return" type="xs:string"/> | |
</xs:sequence> | |
</xs:complexType> | |
</xs:schema> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment