Implementing Java Web Service with xfc

161 Views Asked by At

I'm a newbie developing Web Services. I've created java classes from XSD using xjc command (command line "xjc ....\MySchema.xsd").

<?xml version="1.0" encoding="UTF-8"?>
<!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
<s:schema elementFormDefault="qualified" targetNamespace="http://SAS.Servicios/" xmlns:s="http://www.w3.org/2001/XMLSchema">
    <s:element name="S014">
        <s:complexType>
            <s:sequence>
                <s:element name="MensEntrada">
                    <s:complexType mixed="true">
                        <s:choice maxOccurs="unbounded" minOccurs="0">
                            <s:any processContents="lax"/>
                        </s:choice>
                    </s:complexType>
                </s:element>
            </s:sequence>
        </s:complexType>
    </s:element>
    <s:element name="S014Response">
        <s:complexType>
            <s:sequence>
                <s:element name="MensSalida">
                    <s:complexType mixed="true">
                        <s:choice maxOccurs="unbounded" minOccurs="0">
                            <s:any processContents="lax"/>
                        </s:choice>
                    </s:complexType>
                </s:element>
            </s:sequence>
        </s:complexType>
    </s:element>
</s:schema>

xsd app generates some classes,

S014.java

...
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "mensEntrada"
})
@XmlRootElement(name = "S014")
public class S014 {

    @XmlElement(name = "MensEntrada", required = true)
    protected S014 .MensEntrada mensEntrada;

    public S014 .MensEntrada getMensEntrada() {
        return mensEntrada;
    }

    public void setMensEntrada(S014 .MensEntrada value) {
        this.mensEntrada = value;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "content"
    })
    public static class MensEntrada {
        @XmlMixed
        @XmlAnyElement(lax = true)
        protected List<Object> content;
        public List<Object> getContent() {
            if (content == null) {
                content = new ArrayList<Object>();
            }
            return this.content;
        }
    }
}

S014Response.java

...
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "mensSalida"
})
@XmlRootElement(name = "S014Response")
public class S014Response {

    @XmlElement(name = "MensSalida", required = true)
    protected S014Response.MensSalida mensSalida;

    public S014Response.MensSalida getMensSalida() {
        return mensSalida;
    }

    public void setMensSalida(S014Response.MensSalida value) {
        this.mensSalida = value;
    }


    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "content"
    })
    public static class MensSalida {
        @XmlMixed
        @XmlAnyElement(lax = true)
        protected List<Object> content;
        public List<Object> getContent() {
            if (content == null) {
                content = new ArrayList<Object>();
            }
            return this.content;
        }
    }
}

ObjectFactory.java

...
@XmlRegistry
public class ObjectFactory {

    public ObjectFactory() {
    }

    public S014Response createS014Response() {
        return new S014Response();
    }

    public S014 createS014() {
        return new S014();
    }

    public S014Response.MensSalida createS014ResponseMensSalida() {
        return new S014Response.MensSalida();
    }

    public S014 .MensEntrada createS014MensEntrada() {
        return new S014 .MensEntrada();
    }
}

I need to create a Web Service to read an incoming message "MensEntrada" and returns "MensSalida".

I was trying this:

...
@WebService (serviceName = "S014Service", portName="S014ServiceSoap", targetNamespace = "http://SAS.Servicios/")
public class S014Service extends AcceptMessage {

    public S014Service(WebServiceReceiver webServiceReceiver) {
        super(webServiceReceiver);
    }

    @WebMethod(action = "S014")
    public S014Response S014(@WebParam(name = "MensEntrada") S014 param) {
        String response = param.getMensEntrada().toString();
        response = response + "!!!";
        S014Response ms = new S014Response();
        return ms;
    }    
 }

But it doesn't work. How can I read MensEntrada? How can I write MensSalida?

Thanks in advance!!

1

There are 1 best solutions below

1
On

Finally I got to read MensEntrada "Sometimes". If I use this WebMethod ("S0142"):

@WebMethod(action = "S0142")
public void S0142(@WebParam(name = "param_name") S014 param)
{
    List<Object> ls = param.getMensEntrada().getContent();      
    Node ds = (Node)ls.get(0);
    String response = this.webServiceReceiver.processData(nodeToString(ds));
    List<Object> ls2 = new ArrayList<>();
    Node node = stringToNode(response);
    ls2.add(node);
    S014Response res = new S014Response();
    MensSalida ms = new MensSalida();
    ms.setContent(ls2);
    res.setMensSalida(ms);
    String test = nodeToString((Node)res.mensSalida.getContent().get(0));
} 

It works, and I can set S014Response (thanks to Gil R.)

But If I use another this WebMethod ("S014"):

@WebMethod(action = "S014")
public S014Response S014(@WebParam(name = "MensEntrada") S014 param)
{
    List<Object> ls = param.getMensEntrada().getContent();      
    Node ds = (Node)ls.get(0);
    String response = this.webServiceReceiver.processData(nodeToString(ds));
    List<Object> ls2 = new ArrayList<>();
    Node node = stringToNode(response);
    ls2.add(node);
    S014Response res = new S014Response();
    MensSalida ms = new MensSalida();
    ms.setContent(ls2);
    res.setMensSalida(ms);
    String test = nodeToString((Node)res.mensSalida.getContent().get(0));
    return res;
}  

It doesn't work, because param.getMensEntrada().getContent(); is not readable.

SOAP Envelope for "S0142" is this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sas="http://SAS.Servicios/">
   <soapenv:Header/>
   <soapenv:Body>
      <sas:S0142>
         <param_name>
            <sas:MensEntrada>
                [MyIncomingMessage]
            </sas:MensEntrada>
         </param_name>
      </sas:S0142>
   </soapenv:Body>
</soapenv:Envelope>

SOAP Envelope for "S014" is this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sas="http://SAS.Servicios/">
   <soapenv:Header/>
   <soapenv:Body>
      <sas:S014>
          [MyIncomingMessage]
      </sas:S014>
   </soapenv:Body>
</soapenv:Envelope>

"...<param_name><sas:MensEntrada>...." is not present.

Is this correct to return MyOutgoingMessage?

@WebMethod(action = "S014")
public S014Response S014(@WebParam(name = "MensEntrada") S014 param)

Why it doesn't work?