Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / Java

Consuming WCF Service in Java Client

Rate me:
Please Sign up or sign in to vote.
5.00/5 (15 votes)
23 May 2014CPOL3 min read 167.7K   21   20
This article will talk about how a WCF service can be consumed by a Java client application.

Introduction

WCF is built to support interoperability. Being just a .NET developer, it becomes very difficult to prove this concept. I created this POC with the help of one of my friends Jay D Khambholiya who is a Java developer.

This article will talk about how a WCF service can be consumed by a Java client application in following three steps:

  1. Create a WCF Service
  2. Host WCf Service in IIS
  3. Consume WCF Service in Java Client

Step 1: Create WCF Service

  • Open Visual Studio 2012 > File Menu > click New Project… > select Class Library – CalculatorService > click OK

Image 1

  • Delete the class - Class1.cs file that is auto-generated.
  • Right-click on CalculatorService project > Add > click New Item > select WCF Service > provide Name as CalculatorService > click Add button:

Image 2

  • This will add the required assembly references (like, System.ServiceModel) along with a Service Contract Interface – ICalculatorService.cs and Service class – CalculatorService.cs
  • Modify ICalculatorService.cs as follows:
C#
using System.ServiceModel;
namespace CalculatorService
{
    [ServiceContract]
    public interface ICalculatorService
    {
        [OperationContract]
        int Add(int num1, int num2);
    }
} 
  • Modify CalculatorService.cs as follows:
C#
namespace CalculatorService
{
    public class CalculatorService : ICalculatorService
    {
        public int Add(int num1, int num2)
        {
            return num1 + num2;
        }
    }
}
  • Build the solution by pressing [CTRL]+[SHIFT]+b or go to BUILD menu > click Build Solution option.

Step 2: Host WCF Service in IIS

  • Add a new web site to host Calculator Service in IIS.
  • Go to File Menu > Add > click New Web Site… > select WCF Service > provide a suitable name – CalculatorServiceIISHost > click OK:

Image 3

  • A new web site will be added with a service class - Service.cs and an interface - IService.cs within App_Code folder along with a Service.svc file. Since the service is already created, delete the auto created service class (Service.cs) and the interface (IService.cs) from App_Code folder.
  • Right-click on CalculatorServiceIISHost project > click Add Reference > select CalculatorService > click OK:

Image 4

  • Open Service.svc file > Delete CodeBehind property:
ASP.NET
<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>
  • Modify Service property as shown below:
ASP.NET
<%@ ServiceHost Language="C#" Debug="true" Service="CalculatorService.CalculatorService" %>
  • Modify web.config file to have the following settings:
XML
<system.serviceModel>
    <services>
        <service name="CalculatorService.CalculatorService" 
                 behaviorConfiguration="mex">
            <endpoint address="CalculatorService" binding="basicHttpBinding"
               contract="CalculatorService.ICalculatorService"></endpoint>
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8080/"/>
                </baseAddresses></host>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="mex">
                <serviceMetadata httpGetBinding="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>
  • Now the service is ready to be hosted in IIS.
  • Open IIS > right-click on Default Web Site > click Add Application… > enter Alias name – calculator > select the Physical path of the service > click OK:

Image 5

  • Enable Anonymous Authentication as shown below:

Image 6

  • Go to Content View > right-click Service.svc > click Browse:

Image 7

Step 3: Consume WCF Service in Java client

  • Tools Used: Eclipse Indigo, Apache Tomcat 6, Apache Axis (comes pre-installed in Eclipse Indigo)
  • Create a new Dynamic Web Project in Eclipse:

Image 8

  • To create a WS Client, select New > Web Services > select Web Service Client > click Next button:

Image 9

  • Enter the WSDL path of the WCF Service > click Finish button:

Image 10

  • This should auto-generate the code for the WS proxy:

Image 11

  • Create a simple JSP page that has two text boxes and a servlet to call the “add” method via web service:
  • Write the following code in WCFServlet’s doPost() method to call web service’s “add” method:
Java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
 Integer input1=Integer.parseInt(request.getParameter("input1"));
 Integer input2=Integer.parseInt(request.getParameter("input2"));
 ICalculatorServiceProxy calService=new ICalculatorServiceProxy();
 Integer result=calService.add(input1,input2);
 request.setAttribute("res", "The result is: "+result);
 RequestDispatcher rd=request.getRequestDispatcher("WSDemo.jsp");
 rd.forward(request, response);
}
  • Display the result on the same page WSDemo.jsp:

Image 12

Summary

This article can be referred by any project or individual to get a step-by-step guidance on how a WCF service can be created and hosted in IIS and then create a Java client application to consume the service.

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager
India India
I'm Debabrata Das, also known as DD. I started working as a FoxPro 2.6 developer then there is a list of different technologies/languages C, C++, VB 6.0, Classic ASP, COM, DCOM, ASP.NET, ASP.NET MVC, C#, HTML, CSS, JavaScript, jQuery, SQL Server, Oracle, No-SQL, Node.Js, ReactJS, etc.

I believe in "the best way to learn is to teach". Passionate about finding a more efficient solution of any given problem.

Comments and Discussions

 
QuestionMissing Proxy.java and stub.java after generate WCF in Eclipse Pin
Member 1070646324-Aug-16 19:38
Member 1070646324-Aug-16 19:38 
AnswerRe: Missing Proxy.java and stub.java after generate WCF in Eclipse Pin
Debabrata_Das14-Nov-16 0:25
professionalDebabrata_Das14-Nov-16 0:25 
QuestionServer runtime is blank Pin
Adam Langer11-Aug-15 5:22
Adam Langer11-Aug-15 5:22 
AnswerRe: Server runtime is blank Pin
Debabrata_Das19-Aug-15 7:36
professionalDebabrata_Das19-Aug-15 7:36 
AnswerRe: Server runtime is blank Pin
Jay Khambholiya19-Aug-15 8:47
Jay Khambholiya19-Aug-15 8:47 
QuestionThere is no option to add a web service in Eclipse 4.5. Pin
morganrg30-Jul-15 4:15
morganrg30-Jul-15 4:15 
AnswerRe: There is no option to add a web service in Eclipse 4.5. Pin
Debabrata_Das30-Jul-15 7:16
professionalDebabrata_Das30-Jul-15 7:16 
GeneralRe: There is no option to add a web service in Eclipse 4.5. Pin
morganrg4-Aug-15 7:50
morganrg4-Aug-15 7:50 
GeneralRe: There is no option to add a web service in Eclipse 4.5. Pin
Debabrata_Das6-Aug-15 4:49
professionalDebabrata_Das6-Aug-15 4:49 
QuestionConsuming Java-WS from dotnet Pin
devvvy29-Dec-14 18:47
devvvy29-Dec-14 18:47 
AnswerRe: Consuming Java-WS from dotnet Pin
Debabrata_Das29-Dec-14 21:19
professionalDebabrata_Das29-Dec-14 21:19 
GeneralRe: Consuming Java-WS from dotnet Pin
devvvy1-Jan-15 20:04
devvvy1-Jan-15 20:04 
SuggestionStep 3 Pin
Member 1107539511-Sep-14 4:27
Member 1107539511-Sep-14 4:27 
GeneralRe: Step 3 Pin
Debabrata_Das15-Sep-14 5:51
professionalDebabrata_Das15-Sep-14 5:51 
QuestionWhat if complex type is present in WCF WSDL? Pin
Sándor Hatvani13-Aug-14 2:51
Sándor Hatvani13-Aug-14 2:51 
QuestionCan we use any binding? Pin
Alemvik8-Jul-14 11:07
Alemvik8-Jul-14 11:07 
AnswerRe: Can we use any binding? Pin
Debabrata_Das9-Jul-14 3:34
professionalDebabrata_Das9-Jul-14 3:34 
As per my knowledge, the service has to be WS-I Basic Profile 1.1 compliant so that it becomes portable cross-platforms hence basicHttpBinding is the only binding that can be cosumed in Java client.

Creating WS-I Basic Profile 1.1 Interoperable Services[^]

I just found another stuff called - "WCF Express Interop Bindings 1.0" which provides a starter kit for WCF service developers wishing to connect with Java clients in WebSphere, WebLogic, Metro and Apache.

I have not tried it yet. Please visit the following link for more information:

WCF Express Interop Bindings 1.0[^]
Regards,
DD


modified 9-Jul-14 9:53am.

GeneralRe: Can we use any binding? Pin
Alemvik9-Jul-14 10:39
Alemvik9-Jul-14 10:39 
QuestionConsuming WCF Service in Java Client Pin
prash.ever27-May-14 6:32
prash.ever27-May-14 6:32 
AnswerRe: Consuming WCF Service in Java Client Pin
Debabrata_Das27-May-14 6:35
professionalDebabrata_Das27-May-14 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.