ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-08-25 12:04:33  阅读:198  来源: 互联网

标签:java jaxb web-services 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>

我想跳过这个返回元素.

我想:

<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请求中用子代替arg0,则应在方法参数中添加@WebParam(name =“child”)注释.

您可以通过使用@SOAPBinding(…)注释服务(类)来对生成的样式施加影响 – 使用哪种样式取决于您的需求/威士忌 – 这里请参阅我的第一条评论的链接以查看差异.

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

因为你想摆脱响应中的return元素,请注意这不是WS-I complient,因为只允许一个soap :: body子 – 但是因为这是你的意图你应该更改ParameterStyle.WRAPPED默认值ParameterBtyle.BARE的SOAPBinding参数.但是,这也可能会改变请求类型.也许在这里看看@ResponseWrapper.我还没用过,所以我不能提供详细的信息.

标签:java,jaxb,web-services,cxf
来源: https://codeday.me/bug/20190825/1718705.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有