如何使用JavaWeb服务获得复杂的soap响应?

如何使用JavaWeb服务获得复杂的soap响应?,java,web-services,soap,netbeans,Java,Web Services,Soap,Netbeans,我已经创建了一个web服务来使用netbeans添加两个数字…我得到了以下响应 SOAP Response <?xml version="1.0" encoding="UTF-8"?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:AddResponse xmlns:ns2="http://calculator.me.

我已经创建了一个web服务来使用netbeans添加两个数字…我得到了以下响应

SOAP Response

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:AddResponse xmlns:ns2="http://calculator.me.org/">
            <return>9</return>
        </ns2:AddResponse>
    </S:Body>
</S:Envelope>
SOAP响应
9
Bu我需要生成以下结构的soap响应xml:

<?xml version="1.0" encoding="UTF-8"?>
<response>
<CalculatedValue>9</CalculatedValue>
<response>

9
我的java代码:

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

/**
 *
 * @author User
 */
@WebService(serviceName = "CalculatorWS")
@Stateless()
public class CalculatorWS {

    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "<a>" + txt + "</a>";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "Add")
    public int Add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
        //TODO write your implementation code here:
        int k=i+j;
        return k;

    }
}
导入javax.jws.WebService;
导入javax.jws.WebMethod;
导入javax.jws.WebParam;
导入javax.ejb.Stateless;
/**
*
*@author用户
*/
@WebService(serviceName=“CalculatorWS”)
@无国籍的
公共类计算器{
/**
*这是一个示例web服务操作
*/
@WebMethod(operationName=“hello”)
公共字符串hello(@webgram(name=“name”)字符串txt){
返回“+txt+”;
}
/**
*Web服务操作
*/
@WebMethod(operationName=“添加”)
public int Add(@WebParam(name=“i”)int i,@WebParam(name=“j”)int j){
//要在此处编写实现代码,请执行以下操作:
int k=i+j;
返回k;
}
}
wsdl:



我对此进行了大量搜索……但没有得到任何答案。我在stackoverflow中发现了一篇类似的帖子,但没有正确的答案。如何使用web服务生成特定的soap响应?

不可能得到像您发布的那样的回复。根据定义,SOAP文档(或消息)必须包含
信封
和至少
正文
元素。更多信息请参阅


通过将操作样式指定为RPC(远程过程调用)或document(纯XML),您只能控制
主体
元素的内容.

是否可以至少生成这种类型的响应:9如果我想在同一级别添加一个CalculatedValue\return元素,我将如何实现这一点?@soumitra chatterjee:你的意思是从该方法返回多个值?你可以尝试使用返回结果并返回它。返回值数组。您将在responce中看到多个值
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Metro/2.1.1-b09 (branches/2.1-6834; 2011-07-16T17:14:48+0000) JAXWS-RI/2.2.5-promoted-b04 JAXWS/2.2. -->
<definitions targetNamespace="http://calculator.me.org/" name="CalculatorWS" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:tns="http://calculator.me.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://calculator.me.org/" schemaLocation="CalculatorWS_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="hello">
    <part name="parameters" element="tns:hello"/>
  </message>
  <message name="helloResponse">
    <part name="parameters" element="tns:helloResponse"/>
  </message>
  <message name="Add">
    <part name="parameters" element="tns:Add"/>
  </message>
  <message name="AddResponse">
    <part name="parameters" element="tns:AddResponse"/>
  </message>
  <portType name="CalculatorWS">
    <operation name="hello">
      <input wsam:Action="http://calculator.me.org/CalculatorWS/helloRequest" message="tns:hello"/>
      <output wsam:Action="http://calculator.me.org/CalculatorWS/helloResponse" message="tns:helloResponse"/>
    </operation>
    <operation name="Add">
      <input wsam:Action="http://calculator.me.org/CalculatorWS/AddRequest" message="tns:Add"/>
      <output wsam:Action="http://calculator.me.org/CalculatorWS/AddResponse" message="tns:AddResponse"/>
    </operation>
  </portType>
  <binding name="CalculatorWSPortBinding" type="tns:CalculatorWS">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="hello">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
    <operation name="Add">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="CalculatorWS">
    <port name="CalculatorWSPort" binding="tns:CalculatorWSPortBinding">
      <soap:address location="REPLACE_WITH_ACTUAL_URL"/>
    </port>
  </service>
</definitions>