Home > Net >  Java, apache.CXF plugin - Using local WSDL to consume remotely hosted services
Java, apache.CXF plugin - Using local WSDL to consume remotely hosted services

Time:12-21

Soap services are hosted remotely, but due to some restrictions on our server I got WSDL and stored it locally. Using maven, org.apache.cxf plugin and generate sources command I've generated classes from local WSDL.

WebServiceClient Class properties :

@WebServiceClient(name = "ManageCredit",
                  wsdlLocation = localWSDLAddress,
                  targetNamespace = targetNamespace)
public class ManageCredit extends Service {
    public final static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName(targetNamespace, "ManageCredit");
    public final static QName ManageCreditEndpoint = new QName(targetNamespace, "ManageCreditEndpoint");
    static {
        URL url = null;
        try {
            url = new URL(localWSDLAddress);
        } catch (MalformedURLException e) {
            java.util.logging.Logger.getLogger(ManageCredit.class.getName())
                .log(java.util.logging.Level.INFO,
                     "Can not initialize the default wsdl from {0}", localWSDLAddress);
        }
        WSDL_LOCATION = url;
    }

Client setUp :

ManageCredit manageCreditService = new ManageCredit();
ManageCreditPortType manageCreditPortType = manageCreditService.getManageCreditEndpoint();
SignContractRequest signContractRequest = new SignContractRequest();
signContractRequest.setRequestID("123");
signContractRequest.setApplicationID("1111");
signContractRequest.setChannelID("someValue");
manageCreditPortType.signContract(signContractRequest);

Using this code, calls are made to http://localhost:8080/.... I need to call remote server, for example https://example.google:1820, instead of localhost. How can I change the endpoint that my client calls, without changing wsdl location.

CodePudding user response:

Since you downloaded the WSDL locally, you now have control over it. So it might work if you change the SOAP addresses within the WSDL file itself to point to whichever address you want to call from your client.

Alternatively, you could try with the BindingProvider.ENDPOINT_ADDRESS_PROPERTY as shown here, although from what I remember you need to do this for each request, instead of configuring it for the whole service client:

MyService service = new MyService(wsdlURL, serviceName);
ServicePort client = service.getServicePort();
BindingProvider provider = (BindingProvider)client;
// You can set the address per request here
provider.getRequestContext().put(
     BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
     "http://my/new/url/to/the/service");

CodePudding user response:

You should copy your wsdl file somewhere under the src/main/resources folder so that it is packaged in the jar file for your project (for the example below I use src/main/resources/META-INF/wsdl). When configuring the cxf-codegen-plugin, specify the wsdlLocation configuration value, as shown below.

<plugin>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-codegen-plugin</artifactId>
  <version>3.3.0</version>
  <executions>
    <execution>
      <id>my-wsdl</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>wsdl2java</goal>
      </goals>
      <configuration>
        <encoding>UTF-8</encoding>
        <wsdlOptions>
          <wsdlOption>
            <wsdl>${project.basedir}\src\main\resources\META-INF\wsdl\helloWorld.wsdl</wsdl>
            <wsdlLocation>classpath:/META-INF/wsdl/helloWorld.wsdl</wsdlLocation>
          </wsdlOption>
        </wsdlOptions>
      </configuration>
    </execution>
  </executions>
</plugin>

You generated WebServiceClient code should then load the wsdl from the classpath, similar to this...

@WebServiceClient(name = "HelloWorldService",
                  wsdlLocation = "classpath:/META-INF/wsdl/helloWorld.wsdl",
                  targetNamespace = "http://example.com/helloWorld")
public class HelloWorldService extends Service {

    public final static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName("http://example.com/helloWorld", "HelloWorldService");
    public final static QName HelloWorldProxyPort = new QName("http://example.com/helloWorld", "HelloWorldProxyPort");
    static {
        URL url = HelloWorldService.class.getClassLoader().getResource("/META-INF/wsdl/helloWorld.wsdl");
        if (url == null) {
            url = HelloWorldService.class.getClassLoader().getResource("META-INF/wsdl/helloWorld.wsdl");
        }
        if (url == null) {
            java.util.logging.Logger.getLogger(HelloWorldService.class.getName())
                .log(java.util.logging.Level.INFO,
                     "Can not initialize the default wsdl from {0}", "classpath:/META-INF/wsdl/helloWorld.wsdl");
        }
        WSDL_LOCATION = url;
    }

    ...
}

When using the service, you can set the endpoint address for the service as follows...

HelloWorldService helloWorldService = new HelloWorldService();
HelloWorldPort client = service.getHelloWorldPort();
BindingProvider provider = (BindingProvider)client;
// You can set the address per request here
provider.getRequestContext().put(
     BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
     "http://my/new/url/to/the/service");
  • Related