开发者

migrateing from axis to cxf

开发者 https://www.devze.com 2023-03-13 14:32 出处:网络
I\'m trying to migrate from axis to cxf and I have wsdl files which the plugin generates to java code. I have 2 questions:

I'm trying to migrate from axis to cxf and I have wsdl files which the plugin generates to java code. I have 2 questions:

  1. I have the following block in wsdl file:

    <xsd:element name="sendSms" type="parlayx_sms_send_local_xsd:sendSms"/>
      <xsd:complexType name="sendSms">
        <xsd:sequence>
          <xsd:element maxOccurs="unbounded" minOccurs="1" name="addresses" type="xsd:anyURI"/>
          <xsd:element maxOccurs="1" minOccurs="0" name="senderName" type="xsd:string"/>
          <xsd:element name="message" type="xsd:string"/>
          <xsd:element maxOccurs="1" minOccurs="0" name="receiptRequest" type="parlayx_common_xsd:SimpleReference"/>
        </xsd:sequence>
      </xsd:complexType>
    

    and the anyURI is generated to String instead of URI, does anyone know why?

  2. The axis creates service locator class and binding stub class which allows to add to the web service attributes such as us开发者_开发技巧er name, password, timeout etc. I need to add this attributes since the service is over secured connection and they are not parameters in the web service generated method sendSms. (you can see the definitions below).

    <wsdl:portType name="SendSms">
      <wsdl:operation name="sendSms">
        <wsdl:input message="parlayx_sms_send:SendSms_sendSmsRequest"/>
        <wsdl:output message="parlayx_sms_send:SendSms_sendSmsResponse"/>
        <wsdl:fault message="parlayx_common_faults:ServiceException" name="ServiceException"/>
        <wsdl:fault message="parlayx_common_faults:PolicyException" name="PolicyException"/>
      </wsdl:operation>
    </wsdl:portType>
    
    <xsd:element name="sendSms" type="parlayx_sms_send_local_xsd:sendSms"/>
      <xsd:complexType name="sendSms">
        <xsd:sequence>
          <xsd:element maxOccurs="unbounded" minOccurs="1" name="addresses" type="xsd:anyURI"/>
          <xsd:element maxOccurs="1" minOccurs="0" name="senderName" type="xsd:string"/>
          <xsd:element name="message" type="xsd:string"/>
          <xsd:element maxOccurs="1" minOccurs="0" name="receiptRequest" type="parlayx_common_xsd:SimpleReference"/>
        </xsd:sequence>
      </xsd:complexType>
    

    I didn't find any example that I'm certain it demonstrates how I add these values. Do you know about such example?

Thanks, Daniela


About your first question, CXF uses JAXB (by default) to do the conversion between XML and objects, and that specifies that anyURI is mapped to a String. You have to do the conversion yourself, using new URI(str) if you want a checked exception on an invalid URI, or URI.create(str) if you prefer an unchecked exception. While you can register a custom converter to handle that for you, it seems to me to be just vastly more complicated than doing it yourself; one extra line of code per URI argument (or maybe several if you're using the checked exception route, which is probably formally better).

On your second question, the key is that the client stubs generated by CXF (or any other JAX-WS compliant client side) will also implement the javax.xml.ws.BindingProvider interface. This leads to code a bit like this:

// Use the right things from your tooling here...
SendSMS serviceInstance = new SMSSender().getSendSMSImplPort();

Map<String, Object> requestContext =
        ((BindingProvider) serviceInstance).getRequestContext();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serviceAddress);
requestContext.put(BindingProvider.USERNAME_PROPERTY, username);
requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
// And so on for all the properties you want to configure

Timeouts are a bit different, as in CXF they're configured at the conduit level. The relevant piece of that thread seems to be this snippet (which belongs inside the cxf.xml file):

<http:conduit name="http://131.107.153.205/.*"> 
    <http:client ConnectionTimeout="0" ReceiveTimeout="0"/> 
</http:conduit> 

I don't know exactly how you'd change that to adapt to your requirements (especially the conduit name); this is an area where I've been sticking with the defaults so far to be honest. (You might be able to set it at the code level – see the comments – but it's not particularly clear to me.) Note also in the above that the two timeouts are set by that code to have no timeout at all; that's probably not what you want in production code…


Yes, CXF uses JAXB (by default) to do the conversion between XML and objects, and that specifies that anyURI is mapped to a String. However, it's possible to change binding between Java types and SOAP types. Here is a binding file for JAXB, which maps xs:anyURI to java.net.URI

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.0"
              xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema">

        <jaxb:globalBindings
            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
            xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <jaxb:javaType name="java.net.URI" xmlType="xs:anyURI"
                parseMethod="create" printMethod="toASCIIString" />
        </jaxb:globalBindings>
</jaxb:bindings>

To take this into use give option -b to wsdl2java, which specifies JAXWS or JAXB binding files

wsdl2java -b type-bindings.xml ...
0

精彩评论

暂无评论...
验证码 换一张
取 消