java - 是否可以删除 WS 结果中的包装器元素?

标签 java web-services jaxb cxf

我的 CXF 项目运行良好。

我的WS界面是

package net.betlista.ws;

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

import org.apache.cxf.annotations.EndpointProperty;

@WebService
public interface TestWs {

    @WebMethod
    Result foo(String child);

}

实现是

package net.betlista.ws;

import org.springframework.stereotype.Component;

@Component("testWsEndpoint")
public class TestWsImpl implements TestWs {

    @Override
    public Result foo(final String child) {
        Result res = new Result();
        res.status = "ok";
        res.data = "bar";
        return res;
    }

}

结果类型类:

package net.betlista.ws;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

//@XmlTransient - NOT working
@XmlType
//@SOAPBinding(parameterStyle=SOAPBinding.ParameterStyle.BARE) - NOT working
public class Result {

    @XmlElement
    String status;

    @XmlElement
    String data;

}

当我用请求调用 WS 时:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.betlista.net/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:foo>
         <arg0>a</arg0>
      </ws:foo>
   </soapenv:Body>
</soapenv:Envelope>

结果是:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:fooResponse xmlns:ns2="http://ws.betlista.net/">
         <return>
            <status>ok</status>
            <data>bar</data>
         </return>
      </ns2:fooResponse>
   </soap:Body>
</soap:Envelope>

我想跳过这个 return 元素。

我想要:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:fooResponse xmlns:ns2="http://ws.betlista.net/">
        <status>ok</status>
        <data>bar</data>
      </ns2:fooResponse>
   </soap:Body>
</soap:Envelope>

我找到了 this问题,但如果我使用它,元素 arg 也会从请求中丢失,这是我不想要的。

我尝试将此 @SOAPBinding 注释用于方法(如上所述工作)以及 Result 类型(不工作)。

请求的 WSDL:

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ws.betlista.net/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="TestWsImplService" targetNamespace="http://ws.betlista.net/">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.betlista.net/" elementFormDefault="unqualified" targetNamespace="http://ws.betlista.net/" version="1.0">
<xs:element name="foo" type="tns:foo"/>
<xs:element name="fooResponse" type="tns:fooResponse"/>
<xs:complexType name="foo">
    <xs:sequence>
      <xs:element minOccurs="0" name="arg0" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
<xs:complexType name="fooResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="tns:result"/>
    </xs:sequence>
  </xs:complexType>
<xs:complexType name="result">
    <xs:sequence>
      <xs:element minOccurs="0" name="status" type="xs:string"/>
      <xs:element minOccurs="0" name="data" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
  </wsdl:types>
  <wsdl:message name="foo">
    <wsdl:part element="tns:foo" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="fooResponse">
    <wsdl:part element="tns:fooResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="TestWs">
    <wsdl:operation name="foo">
      <wsdl:input message="tns:foo" name="foo">
    </wsdl:input>
      <wsdl:output message="tns:fooResponse" name="fooResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="TestWsImplServiceSoapBinding" type="tns:TestWs">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="foo">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="foo">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="fooResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="TestWsImplService">
    <wsdl:port binding="tns:TestWsImplServiceSoapBinding" name="TestWsImplPort">
      <soap:address location="http://localhost:8080/tests-wicket-cxf/ws/TestWs"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

最佳答案

如果您想在 SOAP 请求中用 child 替换 arg0,您应该向您的方法参数添加一个 @WebParam(name="child") 注释。

您还可以通过使用 @SOAPBinding(...) 注释服务(类)来进一步影响生成的样式 - 使用哪种样式取决于您的需要/whises - 此处请引用转到我的第一条评论的链接以查看差异。

此外,请查看 CXF docs查看每个 @SOAPBinding 参数的默认值。

因为你想去掉响应中的 return 元素,请注意这不符合 WS-I,因为只允许一个 soap::body 子元素——但这是根据您的意图,您应该将 ParameterStyle.WRAPPED 默认 SOAPBinding 参数更改为 ParameterStyle.BARE。然而,这也可能会改变请求类型。也许在这里看看 @ResponseWrapper。我还没有使用过它,所以我不能提供详细信息。

关于java - 是否可以删除 WS 结果中的包装器元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19678947/

相关文章:

java - JDOM 解析速度比 DOM 慢得多

java - Mockito,@InjectMocks 最终字段的奇怪行为

java - 在 setOnUtteranceProgressListener 中调用时 notifyItemChanged() 不起作用

c# - 大型 WCF Web 服务请求因 (400) HTTP Bad Request 而失败

java - 在 JAXB 编码(marshal)期间禁用 List<T> 实现者的特殊处理

soap - JAXB - 将 SOAP 映射到 Java 类

jaxb - 抑制 Javadoc

java - Process.getInputStream() 不返回所有行

.net - ColdFusion Web 服务 wsdl 元素

java - 从本地mac电脑部署一个java web项目到linux服务器