使用xfc实现Java Web服务

时间:2017-08-29 11:47:01

标签: java web service xjc

我是开发Web服务的新手。 我使用xjc命令从XSD创建了java类(命令行" 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会生成一些类,

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();
    }
}

我需要创建一个Web服务来读取传入的消息&#34; MensEntrada&#34;并返回&#34; MensSalida&#34;。

我正在尝试这个:

...
@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;
    }    
 }

但它不起作用。 我怎么读MensEntrada?我怎么写MensSalida?

提前致谢!!

1 个答案:

答案 0 :(得分:0)

最后我读了MensEntrada“有时候”。 如果我使用此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));
} 

它有效,我可以设置S014Response(感谢Gil R。)

但如果我使用另一个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;
}  

它不起作用,因为param.getMensEntrada()。getContent();是不可读的。

“S0142”的SOAP信封是:

<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>

“S014”的SOAP信封是这样的:

<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.

返回MyOutgoingMessage是否正确?

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

为什么它不起作用?