Web-Service in Java EE

Creating Web-Service in Java Using Eclipse

Just writing a basic small simple step by step implementation for creating web-service in Eclipse and creating client for testing web service. For this one must need to have Java EE version of eclipse.

Setup Application:

   First of all Create a dynamic web project I am naming it as ‘WebServiceTest’.

WSStep1

WSStep2

Creating Java Class As Web Service Provider :-

Now Inside ‘src’ folder just create ‘com’ folder and inside ‘com’ create a folder name as per you, I am just creating ‘bss’. This is just for a good practice as per standards.Now inside ‘bss’ folder I am creating a new class with Name ‘MathService’. Inside this class I am creating a method ‘addNumbers()’ which will be adding two number and will return their submission.

So the Overall Structure will look like below image:-

WSStep3

You have created your web service provider class, As you are using Eclipse, so all next steps are just few mouse clicks.

Building,Deploying and Testing:

Right Click on ‘Java Resources’ folder and go to web service as in below image:-

WSStep4

Select class by clicking on ‘Browse’ button for Service implementation as below:-

WSStep5

Drag both sliders under ‘Test Service’ and ‘Test Client’ to top. Here you can see during the drag that what exactly these are representing.Also make ‘Publish the Web service’ Check box true as below:-

WSStep6

And click on ‘Next’

WSStep7

Again Click on ‘Next’. You will be on below screen:-

WSStep8

Click on ‘Start Server’ button.After this carry on to Click ‘Next’ as in below screens:-

WSStep9

WSStep10

Here in above image you will be see this ‘Launch’ button, currently don’t click on this. I will be explain at last that what is purpose of this.

WSStep11

WSStep12

WSStep13

Finally in above screen click on ‘Finish’.After clicking on Finish you will be see what you need. You can see in browser that .jsp file is there for testing your web service, you can test your methods and see expected result as in below images:-

WSStep14

WSStep15

So that’s all here. But don’t forgot to explore each and every class which are created inside ‘WebServiceTest‘ and ‘WebServiceTestClient‘ for actual implementation.

One additional test approach is also there:-

As above at once place there was a button ‘Launch’,by clicking on that also we can test  web service,in that case you can see result as below:-

WsdlTestStep1

WsdlTestStep2

Hope this will help someone.

Enjoy………………

Just want to add an additional information that some times  during this exercise you may find an Exception like this:-

java.lang.Exception: Couldn’t find a matching Java operation for WSDD operation YourMehodName

This is bit confusing….. The only reason for this is that inside your .java class you have written method name starting with capital letter.If in our case we have written method name as AddNumbers() then we will get this error. So for avoiding this simply make first character small in your method name.

Below is full trace of this Exception:-

java.lang.Exception: Couldn’t find a matching Java operation for WSDD operation “addNumbers” (2 args)  at org.apache.axis.InternalException.<init>(InternalException.java:71)  at org.apache.axis.description.JavaServiceDesc.loadServiceDescByIntrospection(JavaServiceDesc.java:902)  at org.apache.axis.providers.java.JavaProvider.initServiceDesc(JavaProvider.java:477)  at org.apache.axis.handlers.soap.SOAPService.getInitializedServiceDesc(SOAPService.java:286)  at org.apache.axis.deployment.wsdd.WSDDService.makeNewInstance(WSDDService.java:500)  at org.apache.axis.deployment.wsdd.WSDDDeployableItem.getNewInstance(WSDDDeployableItem.java:274)  at org.apache.axis.deployment.wsdd.WSDDDeployableItem.getInstance(WSDDDeployableItem.java:260)  at org.apache.axis.deployment.wsdd.WSDDDeployment.getService(WSDDDeployment.java:427)  at org.apache.axis.configuration.FileProvider.getService(FileProvider.java:231)  at org.apache.axis.AxisEngine.getService(AxisEngine.java:311)  at org.apache.axis.MessageContext.setTargetService(MessageContext.java:756)  at org.apache.axis.handlers.http.URLMapper.invoke(URLMapper.java:50)  at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)  at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)  at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)  at org.apache.axis.server.AxisServer.invoke(AxisServer.java:239)  at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)  at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)  at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)  at java.lang.Thread.run(Unknown Source)

 

 

Keep Coding……….