Web Services

Java-WS and dotnet Interop Example

This article will show how to create a simple Java Web Service hosted in Tomcat (running on Windows), and consume the Java-WS from dotnet.

STEP 1. Download Tomcat

http://tomcat.apache.org/tomcat-7.0-doc/setup.html#Windows

REF: https://www.youtube.com/watch?v=bP66y108xAc

STEP 2. Set Environment Variables from Computer (Right click) \ Properties \ Advanced system Settings:

JAVA_HOME                        C:\Program Files\Java\jdk1.7.0_45

CATALINA_HOME            C:\apache-tomcat-6.0.43

Open *new* command prompt (existing command prompt wouldn’t see the new additions), verify settings correct:

echo %JAVA_HOME%

                                echo %CATALINA_HOME%

STEP 3. Start Tomcat

From command prompt, navigate to C:\apache-tomcat-6.0.43\bin

Then, from command prompt type: startup.bat

STEP 4. Verify it’s running

From browser:

http://localhost:8080

                http://localhost:8080/examples/servlets/

From command prompt:

netstat -a

(You should notice port 8080 is taken)

 

STEP 5. Now, from NetBeans IDE, New Project

JavaWS-dotnet-interop.NetBeanIDE.NewProj

JavaWS-dotnet-interop.NetBeanIDE.NewProj2

 

STEP 6. Add new Web Service

Simply enter Web Service Name “HellowWorldWebService” and Package “com.helloworld”, keep everything else default:

JavaWS-dotnet-interop.NetBeanIDE.NewWs

JavaWS-dotnet-interop.NetBeanIDE.NewWs2

 

STEP 7. Test

Build the project:

JavaWS-dotnet-interop.NetBeanIDE.Build1

Then Deploy (To Tomcat),

JavaWS-dotnet-interop.NetBeanIDE.Deploy

And finally, “Test Web Service” (By default when you create the web service, it’d create a “Hello” method automatically)

JavaWS-dotnet-interop.NetBeanIDE.TestWebService

From your browser:

JavaWS-dotnet-interop.NetBeanIDE.TestWebService2

You’d later from Visual Studio add Service Reference to http://localhost:8080/HelloJavaWs/HellowWorldWebService?wsdl

 

STEP 8. At this point, with Tomcat running (And NetBeans IDE closed), you can create a new Console Application project from Visual Studio, then add Service Reference to http://localhost:8080/HelloJavaWs/HellowWorldWebService?wsdl

JavaWS-dotnet-interop.VS.Step1

JavaWS-dotnet-interop.VS.Step2

 

Now, run on debugger:

JavaWS-dotnet-interop.VS.Step3

So, you think we’re done? No… NetBeans has a bug which will haunt you when you try deploy to your production server!

http://forums.netbeans.org/topic7615.html

How to deploy Tomcat and the Java Web Service on a Linux box? First you may want to try how to setup a Fedora VM with FTP: https://gridwizard.wordpress.com/2014/12/26/setup-linux-vm-with-sftp-guide-for-msdev

Then, you’d need install Tomcat & Java on your Linux box: https://gridwizard.wordpress.com/2014/12/28/install-tomcat-on-fedora-vm-for-msdev

Then the rest of the steps are trivial – http://stackoverflow.com/questions/2511547/how-to-manually-deploy-a-web-service-on-tomcat-6

I recap the steps here from the above Stackoverflow post, thanks to Thanh Phong (Just in case if someone deletes it!)

1. create the following dir c:\java\src\ws

2. create thew following file c:\java\src\ws\Adder.java

// c:\java\src\ws\Adder.java
package ws;
import javax.jws.WebService;

@WebService
public class Adder {
 public double add( double value1, double value2 ) {
  return value1 + value2;
 }
}

3. standing at c:\java\src\ execute

c:\java\src> javac ws\Adder.java

file c:\java\src\ws\Adder.class will be generated

4. create the following directory structure with the following files

c:\tomcat6\webapps\adder_ws

META-INF
  context.xml
WEB-INF
  classes
    ws
      Adder.class
  lib
    activation.jar
    webservices-api.jar
    webservices-extra.jar
    webservices-extra-api.jar
    webservices-rt.jar
    webservices-tools.jar
  sun-jaxws.xml
  web.xml

@msdev, you can download the jar files here 
http://www.java2s.com/Code/Jar/CatalogJar.htm
http://download.java.net/maven/1/javax.activation/jars/

5. copy compiled file

copy c:\java\src\ws\Adder.class c:\tomcat6\webapps\adder_ws\WEB-INF\classes\ws\Adder.class

6. c:\tomcat6\webapps\adder_ws\META-INF\context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/adder_ws"/>

7. c:\tomcat6\webapps\adder_ws\WEB-INF\web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Adder</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Adder</servlet-name>
        <url-pattern>/add</url-pattern>
    </servlet-mapping>
<!-- not needed
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
-->
</web-app>

8. Config WEB-INF\sun-jaxws.xml

file : c:\tomcat6\webapps\adder_ws\WEB-INF\sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
  <endpoint implementation="ws.Adder" name="Adder" url-pattern="/add"/>
</endpoints>

9. Copy libraries

files at c:\tomcat6\webapps\adder_ws\WEB-INF\lib

copy netbeans files from

[netbeans dir]\enterprise\modules\ext\metro\*.*

and

[netbeans dir]\ide\modules\ext\jaxb\activation.jar

10. restart apache

Shutdown : c:\tomcat6\bin\shutdown.bat

Startup : c:\tomcat6\bin\startup.bat

11. Test

Open a web browser and go to http://localhost:8080/adder_ws/add?wsdl you can also use a tool like soapui (http://www.soapui.org/) to test the web service

Happy Coding!

Advertisements