Friday, July 19, 2013

How to create JAX-RPC webservice Client with Netbeans

The JAX-RPC is  the old encoded style for webservice, netbeans doesn’t support this style anymore. The default webservice style is JAX-WS.  We need to install extra plugin if we want to call the RPC style webservice.

For the normal java application, we can find the solution here:

After the above steps, we can get two folders : ‘Generated Sources’ and ‘Web Service References’. Go to the class where you are going to call the method from the webservice, then right click, you can insert code. That is it.

For maven project, there are several ways to call RPC webservice: axis, ant and CFX.
I used axis to call the service for my project.

Step 1:
Download the wsdl files, and put this file in the folder “src/main.resources/META-INF/wsdl”.
You can get this file by creating the webservice client in the normal java application project, then copy this folder to the maven project.

Step 2:
Define the pom file. Here is my POM file: 

<groupId>com.medallion</groupId>
    <artifactId>ClientWS_Maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ClientWS_Maven</name>
    <url>http://maven.apache.org</url>

    <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <targetJdk>1.6</targetJdk>
    </properties>
   
    <dependencies>
       
    <dependency>
   <groupId>javax.activation</groupId>
   <artifactId>activation</artifactId>
   <version>1.1</version>
       
    </dependency>

       <dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.3</version>
    </dependency>
   
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>3.8.1</version>
           <scope>test</scope>
       </dependency>
       
       <dependency>
           <groupId>org.apache.axis</groupId>
           <artifactId>axis</artifactId>
           <version>1.4</version>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.apache.axis</groupId>
           <artifactId>axis-jaxrpc</artifactId>
           <version>1.4</version>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>commons-discovery</groupId>
           <artifactId>commons-discovery</artifactId>
           <version>0.4</version>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>wsdl4j</groupId>
           <artifactId>wsdl4j</artifactId>
           <version>1.6.2</version>
           <scope>compile</scope>
       </dependency>
    </dependencies>
    <build>
       <sourceDirectory>src/main/java</sourceDirectory>
       <resources>
           <resource>
               <directory>src/main/resources</directory>
           </resource>
           <resource>
               <directory>src/main/autogen</directory>
           </resource>
       </resources>
         
       <plugins>      
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>2.3.2</version>
               <configuration>            
                   <source>1.6</source>
                   <target>1.6</target>
               </configuration>
           </plugin>
           <plugin>
               <groupId>org.codehaus.mojo</groupId>
               <artifactId>axistools-maven-plugin</artifactId>
               <version>1.4</version>
               <executions>
                   <execution>
                       <id>ax-ws-autogen</id>
                       <phase>generate-sources</phase>
                       <goals>
                           <goal>wsdl2java</goal>
                       </goals>
                   </execution>
               </executions>
               <configuration>
                   <useEmitter>true</useEmitter>
                   <packageSpace>com.medallion.clientws_maven</packageSpace>
                   <sourceDirectory>src/main/resources/META-INF/wsdl</sourceDirectory>
                   <wsdlFiles>
                       <wsdlFile>cminterface.asmx.wsdl</wsdlFile>                           
                   </wsdlFiles>
                   <outputDirectory>src/main/autogen</outputDirectory>
               </configuration>
           </plugin>
          
       </plugins>
    </build>   
</project>


step3:
Right click on the project node, test it.

Step4:

Go to the folder src/main/autogen/, you can find the generated code.

Step5:
We can call the webservice now. We used wsdl2java to generate java file locally, then we can call it directly just like calling the local class.

public class App
{
    public static void main( String[] args ) throws ServiceException, RemoteException
    {
       
        com.medallion.clientws_maven.CMInterface cmInterface = new com.medallion.clientws_maven.CMInterfaceLocator();
        com.medallion.clientws_maven.CMInterfaceSoap_PortType stub = cmInterface.getCMInterfaceSoap();
        
        stub.login("User", "password");
        
    }
}
We can also call the webservice directly.. Please refer this log:

No comments:

Post a Comment