Tutorial Java SOAP Webservices JAS-WS with Eclipse J2EE IDE and Tomcat Server Part 1
This is an easy, quick example to get started with Java Web-services (SOAP - Jax-ws) with Eclipse IDE and Tomcat server.
I have divided the tutorial into two parts :
- Creating the WebService.
- Creating Client to utilize the WebService.
Before we start here are my configuration details,
- IDE : Eclipse Indigo J2EE version
- JRE : Java 1.7
- Web Server : Apache Tomcat ver. apache-tomcat-7.0.61
- Operating system : Mac OS X 10.10 Yosemite (We need to use Terminal/Command Prompt to execute WebServices commands to generate files)
jaxb-api.jar
jaxb-impl.jar
jaxws-rt.jar
stax-ex.jar
streambuffer.jar
Let's create a simple Web service that gives book title details based on the ISBN number provided as input.
- Create a Dynamic web Project: WebServicesWithTomcat
- Place the above-mentioned jars under projects WebContent/WEB-INF/lib directory.
- Create package : org.code2care.webservices under projects Java Resources/src directory
- Create an Interface with name BookService that has an abstract method called getBookDetails, BookService.java
- Nows in the same package lets create the implementation class BookServiceImpl, BookServiceImpl.java
- Now let's create web.xml file under WebContent/WEB-INF directory, web.xml
- Create sun-jaxws.xml under directory WebContent/WEB-INF,
- Right Click on the Project folder and Run on Server to generate the .class files. Make sure that you have setup Apache Tomcat Server with Eclipse.
- The .class files for the project will be created, to get these files BookService.class and BookServiceImpl.class files, we need to move to Navigator view. If you cannot see this tab next to Project Explorer, Go to Window -> Show View -> Navigator.
- Go to build -> classes -> org and copy the org directory (with its subdirectories) and paste this folder in some location say c:/wsfiles if on windows or /User/<user-name>/wsfiles.
- Now we have to execute the wsgen command to generate the web services source files.
- Create a org.code2care.webservices.jaxws package under src directory and copy these two generated java files here (note not the .class file)
- Now to deploy the project (war) on Tomcat simply run the project by Right Click on Project -> Run on Server.
- Go to URL: http://localhost:8080/WebServicesWithTomcat/WebServicesWithTomcat, you should see something like :
- The service URL: http://localhost:8080/WebServicesWithTomcat/WebServicesWithTomcat?wsdl will return the xml WSDL description file
package org.code2care.webservices;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface BookService {
@WebMethod
String getBookDetails(int isbn);
}
package org.code2care.webservices;
import javax.jws.WebService;
@WebService(endpointInterface ="org.code2care.webservices.BookService")
public class BookServiceImpl implements BookService {
@Override
public String getBookDetails(int isbn) {
switch (isbn) {
case 101:
return "Java";
case 102:
return "PHP";
case 103:
return "AJAX";
case 104:
return "JavaScript";
case 105:
return "jQuery";
case 106:
return "XML";
case 107:
return "JSON";
case 108:
return "PERL";
case 109:
return ".NET";
case 110:
return "C#";
case 111:
return "RUBY";
case 112:
return "C++";
default:
return "Invalid ISBN Number!";
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>WebServicesWithTomcat</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>WebServicesWithTomcat</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebServicesWithTomcat</servlet-name>
<url-pattern>/WebServicesWithTomcat</url-pattern>
</servlet-mapping>
</web-app>
wsgen -cp . -keep org.code2care.webservices.BookServiceImpl
Move to the wsfiles directory from the command prompt/terminal (using cd command) and execute the above command. The WebService java files GetBookDetails.java and GetBookDetailsResponse.java files should be created under org/code2care/webservices/jaxws directory under wsfiles.
Address: http://localhost:8080/WebServicesWithTomcat/WebServicesWithTomcat
WSDL: http://localhost:8080/WebServicesWithTomcat/WebServicesWithTomcat?wsdl
Port QName: {http://webservices.code2care.org/}BookServiceImplPort
Implementation class: org.code2care.webservices.BookServiceImpl
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1-b03-.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1-b03-.
-->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://webservices.code2care.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservices.code2care.org/"
name="BookServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservices.code2care.org/"
schemaLocation="http://localhost:8080/WebServicesWithTomcat/WebServicesWithTomcat?xsd=1"/>
</xsd:schema>
</types>
<message name="getBookDetails">
<part name="parameters" element="tns:getBookDetails"/>
</message>
<message name="getBookDetailsResponse">
<part name="parameters" element="tns:getBookDetailsResponse"/>
</message>
<portType name="BookService">
<operation name="getBookDetails">
<input message="tns:getBookDetails"/>
<output message="tns:getBookDetailsResponse"/>
</operation>
</portType>
<binding name="BookServiceImplPortBinding" type="tns:BookService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="getBookDetails">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="BookServiceImplService">
<port name="BookServiceImplPort" binding="tns:BookServiceImplPortBinding">
<soap:address location="http://localhost:8080/WebServicesWithTomcat/WebServicesWithTomcat"/>
</port>
</service>
</definitions>
That's it! In the next part, we will see how to utilize this Web Service.
HashTags : #WebServices #Java #WSDL #Eclipse #Tomcat #TutorialMore Posts related to Java,
- Java XML-RPC 3.1.x based web service example
- List of jars required for Struts2 project
- list of jars required for hibernate 4.x.x
- How to create StackOverflow error in java
- Tutorial Java SOAP WebServices JAS-WS with Eclipse J2EE IDE and Tomcat Server Part 1
- import servlet API to eclipse project (javax.servlet cannot be resolved error)
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- Convert String to int in Java
- JSP Hello World Program Tutorial with Eclipse and Tomcat Server
- SharePoint Open in the client application document opens in browser
- Java: TimeZone List with GMT/UTC Offset
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Remove Trailing zeros BigDecimal Java
- [Solution] Java Error Code 1603. Java Update did not complete.
- Maven Eclipse (M2e) No archetypes currently available
- error: file not found: HelloWorld.java
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to check if Java main thread is alive
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- Java XML-RPC java.net.BindException: Address already in use
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end users experience
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- List of jar files for Jax-ws (SOAP) based Java Web Services
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Maven Unsupported major.minor version 51.0
- hibernate.cfg.xml Configuration and Mapping xml Example
- How to verify if java is installed on the computer and get version detail
- Unhandled exception type InterruptedException : Java Threads
- XmlRpcException ConnectException connection refused error
- Error: Unable to access jarfile jarFileName.jar file [Windows]
- BeanDefinitionStoreException IOException parsing XML document from class path resource [spring.xml]
- How to serialize-deserialize an object in java
- List of Java Keywords
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Struts 2 Hello World Example in Eclipse
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- List of Java versions
- Create simple struts2 project using maven commands
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- Error: Can not find the tag library descriptor for
- connection.url property value in hibernate.cfg.xml for mysql
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Setting Java_Home Environment variable on Windows Operating System
More Posts:
- Facebook Thanks for stopping by! We hope to see you again soon. - Facebook
- Android EditText Cursor Colour appears to be white - Android
- Disable EditText Cursor Android - Android
- Connection Failed: 1130 PHP MySQL Error - MySQL
- SharePoint Managed Metadata Hidden Taxonomy List - TaxonomyHiddenList - SharePoint
- Execute .bin and .run file Ubuntu Linux - Linux
- Possible outages message Google Webmaster tool - Google
- Android : Remove ListView Separator/divider programmatically or using xml property - Android
- Unable to edit file in Notepad++ - NotepadPlusPlus
- SharePoint PowerShell Merge-SPLogFile filter by time using StartTime EndTime - SharePoint
- SQLite Error: unknown command or invalid arguments: open. Enter .help for help - Android
- JBoss stuck loading JBAS015899: AS 7.1.1.Final Brontes starting - Java
- Android Wifi WPA2/WPA Connects and Disconnects issue - Android
- Android Toolbar example with appcompat_v7 21 - Android
- ERROR x86 emulation currently requires hardware acceleration. Intel HAXM is not installed on this machine - Android