如何在JAVA SOAP

时间:2015-10-02 09:19:58

标签: java soap

我有一个SOAP Web服务代码,如下所示:

public void sumAndMultiplyAndReference
(@WebParam(name = "firstnumber")int a,
 @WebParam(name = "firstnumber")int b,
 @WebParam(name = "referencenumber")int c
 @WebParam(name = "sum", mode = WebParam.Mode.OUT) Holder<Integer> sum,
 @WebParam(name = "multiply", mode = WebParam.Mode.OUT) Holder<Integer> multiply,
 @WebParam(name = "reference", mode = WebParam.Mode.OUT) Holder<Integer> reference){
sum.value = a + b;
multiply.value = a * b;
reference.value = c;
}

工作正常。但是在代码中应该改变什么,请求参数name = referencenumber和reply参数名是一样的呢?

注意:此处 - 请求参数name = referencenumber

响应参数name = reference

2 个答案:

答案 0 :(得分:2)

这对我有用:

package org.toto.bla;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import javax.xml.ws.Holder;
@WebService
public class WSTest {
    @WebMethod
    public void sumAndMultiplyAndReference(@WebParam(name = "firstnumber") int a, @WebParam(name = "secondnumber") int b,
                                           @WebParam(name = "referencenumber") int c,
                                           @WebParam(name = "sum", mode = WebParam.Mode.OUT) Holder<Integer> sum,
                                           @WebParam(name = "multiply", mode = WebParam.Mode.OUT) Holder<Integer> multiply,
                                           @WebParam(name = "referencenumber", mode = WebParam.Mode.OUT) Holder<Integer> reference) {
        sum.value = a + b;
        multiply.value = a * b;
        reference.value = c;
    }
}

以下是生成的XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:ns0="http://bla.toto.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://exporter.vas.asgs.atos.net/">
   <xsd:complexType name="sumAndMultiplyAndReferenceResponse">
      <xsd:sequence>
         <xsd:element name="sum" type="xsd:int" minOccurs="0"/>
         <xsd:element name="multiply" type="xsd:int" minOccurs="0"/>
         <xsd:element name="referencenumber" type="xsd:int" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="sumAndMultiplyAndReference">
      <xsd:sequence>
         <xsd:element name="firstnumber" type="xsd:int"/>
         <xsd:element name="secondnumber" type="xsd:int"/>
         <xsd:element name="referencenumber" type="xsd:int"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="sumAndMultiplyAndReferenceResponse" type="ns0:sumAndMultiplyAndReferenceResponse"/>
   <xsd:element name="sumAndMultiplyAndReference" type="ns0:sumAndMultiplyAndReference"/>
</xsd:schema>

您还可以使用INOUT参数类型:     包org.toto.bla;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import javax.xml.ws.Holder;

@WebService
public class WSTest {
    @WebMethod
    public void sumAndMultiplyAndReference(@WebParam(name = "firstnumber") int a, @WebParam(name = "secondnumber") int b,
                                           @WebParam(name = "sum", mode = WebParam.Mode.OUT) Holder<Integer> sum,
                                           @WebParam(name = "multiply", mode = WebParam.Mode.OUT) Holder<Integer> multiply,
                                           @WebParam(name = "referencenumber", mode = WebParam.Mode.INOUT) Holder<Integer> c) {
        sum.value = a + b;
        multiply.value = a * b;
    }
}

生成完全相同的XSD。

答案 1 :(得分:1)

使用WebParam.Mode.INOUT,这样您就可以双向使用它。

阅读from Apache about @WebParam,下面摘录。

  

如果持有者被指定为WebParam.Mode.INOUT参数,那么   客户端也可以使用它们来发送数据和应用程序。该   然后将使用来自的数据初始化Holder实例   客户要求。应用程序最终可以检查数据   用响应值重写它。