Today let's try out an example on how to create a Java SOAP based Web Service also known as JAX-WS using Eclipse. We will build a web service that will accept an Employee Salary object or POJO (EmpSal) which contains Basic Salary & Variable Salary components and add those and set the result as CTC and return back the EmpSal object to the client.
Developed with:
First create the EmpSal class, which we will actually use to transfer from client to service and back:
Next, create the interface for the webservice which defines the blue print of the service:
Create the web service class which implements the interface with the underlying Business logic. Note that this example implements Base64 Basic decoding, thus the web service client is expected to pass the authentication to be able to get a valid response from the web service.
Now, Create a Dynamic Web project in Eclipse
Add the following configurations in the web.xml of the project
Add the Java project created in the beginning to the Dynamic Web project as follows: Project Properties -> Deployment Assembly -> Add
Deploy the Dynamic Web Project on JBoss and hit the following URL on your browser for the WSDL of your Web service you have just created:
http://localhost:8080/CalcCTCWebServicePublisher/ws/CTCService?wsdl
You should be able to see the WSDL of your web service, and we will use the same to create a SOAP client to talk to our newly created web service.
Finally, Create a Web Service Client project in Eclipse
Create a New "Web Service Client" project in eclipse and provide the WSDL url as shown above. Provide a client project name and set the the client project type as "Java Utility Project" which basically means a standalone Java program.
The above steps will automatically create Interfaces and Implementations of the Web Service and Web Service Proxy also the Service Locator and Soap Binding Stub class along with the Employee class.
You would require to add a class with main() for calling the web service as follows:
Now start the Dynamic Web Project as you deploy it on JBoss, and execute the Client code, WSClient.java. The client will send and employee object wrapped in SOAP envelope to the Web service and in return receive the same object back with CTC calculated and set in the employee object.
Developed with:
- JBoss 6
- Apache commons codec jar (For Base64 encoding)
- Eclipse Juno
First create the EmpSal class, which we will actually use to transfer from client to service and back:
Next, create the interface for the webservice which defines the blue print of the service:
Create the web service class which implements the interface with the underlying Business logic. Note that this example implements Base64 Basic decoding, thus the web service client is expected to pass the authentication to be able to get a valid response from the web service.
Now, Create a Dynamic Web project in Eclipse
Add the following configurations in the web.xml of the project
Add the Java project created in the beginning to the Dynamic Web project as follows: Project Properties -> Deployment Assembly -> Add
Deploy the Dynamic Web Project on JBoss and hit the following URL on your browser for the WSDL of your Web service you have just created:
http://localhost:8080/CalcCTCWebServicePublisher/ws/CTCService?wsdl
You should be able to see the WSDL of your web service, and we will use the same to create a SOAP client to talk to our newly created web service.
Finally, Create a Web Service Client project in Eclipse
Create a New "Web Service Client" project in eclipse and provide the WSDL url as shown above. Provide a client project name and set the the client project type as "Java Utility Project" which basically means a standalone Java program.
The above steps will automatically create Interfaces and Implementations of the Web Service and Web Service Proxy also the Service Locator and Soap Binding Stub class along with the Employee class.
You would require to add a class with main() for calling the web service as follows:
Now start the Dynamic Web Project as you deploy it on JBoss, and execute the Client code, WSClient.java. The client will send and employee object wrapped in SOAP envelope to the Web service and in return receive the same object back with CTC calculated and set in the employee object.
Download WebService Client Project Download WebService Project |
Getting below erreor while running the client.please help....
ReplyDeleteAxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server
faultSubcode:
faultString: org/apache/commons/codec/binary/Base64
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}stackTrace:org/apache/commons/codec/binary/Base64
at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at com.egi.demo.webservice.CalcCTCWebServiceImplServiceSoapBindingStub.calcCTC(CalcCTCWebServiceImplServiceSoapBindingStub.java:158)
at com.egi.demo.webservice.WSClient.main(WSClient.java:20)
{http://xml.apache.org/axis/}hostname:BIPRARAJ-HP
I want soap web-service example program with screen-shots, can anyone help..
ReplyDeleteMan you save me! I was trying to learn how to pass username and password in my java cliente and i founded you article and you save, although I dont' understand you it Works, but it Works... Thank you!!!!
ReplyDeletei want soap web services which allow user to review, edit or delete review
ReplyDeleteTks
ReplyDelete