Skip to content

Instantly share code, notes, and snippets.

@juliangamble
Last active August 29, 2015 14:23
Show Gist options
  • Save juliangamble/f098c21f8dd7966d67ba to your computer and use it in GitHub Desktop.
Save juliangamble/f098c21f8dd7966d67ba to your computer and use it in GitHub Desktop.

Ensure you understand the difference between doc literal rpc format:

The URLs may look like: Doc Literal: https://mydomain.com/WebService?wsdl

RPC: https://mydomain.com/WebService?wsdl=WebService.wsdl

The contents will look like:

Document Literal:

<wsdl:definitions xmlns:ns1="http://mydomain.com/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://mydomain.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WebService" targetNamespace="http://mydomain.com/">
  <wsdl:import location="http://mydomain.com/WebService?wsdl=WebService.wsdl" namespace="http://mydomain.com/"></wsdl:import>
  <wsdl:binding name="FormsServerWebServiceImplServiceSoapBinding" type="ns1:FormsServerWebService">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  <wsdl:operation name="MyAction">
  <soap:operation soapAction="" style="document"/>
  <wsdl:input name="MyAction">
    <soap:body use="literal"/>
  </wsdl:input>

...

This is RPC:

<wsdl:definitions xmlns:ns1="http://mydomain.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WebService" targetNamespace="http://mydomain.com/">
  <wsdl:types>
    <xs:schema xmlns:tns="http://mydomain.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://mydomain.com/">
    <xs:complexType name="myAction">
      <xs:sequence>
        <xs:element minOccurs="0" name="arg1" type="xs:string"/>
        <xs:element minOccurs="0" name="arg2" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>

Note that you need to save the RPC version in your project.

To generate the corresponding JAXB classes - use a maven pom.xml file like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>mygroup</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.8.3</version>
                <executions>
                    <execution>
                        <id>my_wsdl</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <wsdl>true</wsdl>
                            <schemaDirectory>${project.basedir}/src/main/resources/schema/</schemaDirectory>
                            <generateDirectory>${project.build.directory}/generated/java</generateDirectory>
                            <schemaIncludes>
                                <include>wsdls/WebService.wsdl</include>
                            </schemaIncludes>
                            <bindingDirectory>${project.basedir}/src/main/resources/binding</bindingDirectory>
                            <bindingIncludes>
                                <include>myjaxbbindings.xjb</include>
                            </bindingIncludes>
                            <forceRegenerate>true</forceRegenerate>
                            <args>
                                <arg>-Xannotate</arg>
                            </args>
                            <plugins>
                                <plugin>
                                    <groupId>org.jvnet.jaxb2_commons</groupId>
                                    <artifactId>jaxb2-basics</artifactId>
                                    <version>0.6.0</version>
                                </plugin>
                                <plugin>
                                    <groupId>org.jvnet.jaxb2_commons</groupId>
                                    <artifactId>jaxb2-basics-annotate</artifactId>
                                    <version>0.6.0</version>
                                </plugin>
                            </plugins>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/generated/java</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <debug>true</debug>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <includes>
                        <include>**/*.class</include>
                        <include>**/*.index</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Then setup a JAXB binding file that looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:annox="http://annox.dev.java.net"
        xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
        jaxb:extensionBindingPrefixes="xjc annox"
        version="2.1">
    <jaxb:bindings schemaLocation="../schema/wsdls/WebService.wsdl" node="//xs:schema">
        <jaxb:bindings node="xs:complexType[@name='myAction']">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="MyAction" namespace="http://mydomain.com/"/>
            </annox:annotate>
        </jaxb:bindings>
        <jaxb:bindings node="xs:complexType[@name='myActionResponse']">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="MyActionResponse" namespace="http://mydomain.com/"/>
            </annox:annotate>
        </jaxb:bindings>

Then generate the source using

mvn generate-sources

Then setup a Java class to call the generated sources that looks like this:

package com.mycompany;

import com.mydomain.*;
import com.mydomain.Exception;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.client.core.WebServiceTemplate;

import javax.xml.bind.JAXBElement;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;


public class CallWebServiceUsingWebServiceTemplate {

    public static void main(String args[]) {
        List<String> result = null;

        MyAction request = new ObjectFactory().createMyAction();

        request.setArg1("arg1");

        WebServiceTemplate webServiceTemplate = getWebServiceTemplate();

        JAXBElement elem = (JAXBElement)webServiceTemplate.marshalSendAndReceive(request);
        MyActionResponse response =
                (MyActionResponse) elem.getValue();

        result = response.getReturn();

    }

    private static WebServiceTemplate getWebServiceTemplate() {
        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();

        Jaxb2Marshaller marshaller1 = new Jaxb2Marshaller();
        marshaller1.setContextPath("com.mydomain");

        webServiceTemplate.setMarshaller(marshaller1);
        webServiceTemplate.setUnmarshaller(marshaller1);

        webServiceTemplate.setDefaultUri("https://mydomain.com/WebService");
        return webServiceTemplate;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment