WebService using Apache CXF/Tomcat
- By using the JAX-WS application, we will use the Apache CXF-first approach like the earlier POJO application.
- First, we annotate the interface with a @WebService tag. After that, we will implement this interface.
Let’s Implementing Web Interface, the implementation of the web interface is shown here:
HelloWorld1.java
public class HelloWorld1 implements HelloWorld { @Override public String demo(String name) { return ("hello " + name); } }
The demo method is annotated with the @Override tag. The method returns a “hello” message to the caller.
Server1.java
public class Server1 { public static void main(String[] arg) throws Exception { HelloWorld implementor = new HelloWorld(); Endpoint.publish("http://localhost:9090/HelloServerPort", implementor, new LoggingFeature()); System.out.println("Server is ready..."); Thread.sleep(4 * 60 * 1000); System.out.println("Server is existing ..."); System.exit(0); } }
- If we want to deploy our server, we need to make a few more modifications to our project, as listed below.
- Deploying Server
- Finally, we need to deploy the server application, so we need to make one more modification in pom.xml
The code what we need to add into your pom.xml is given below −
<profiles> <profile> <id>server</id> <build> <defaultGoal>test</defaultGoal> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass> com.demo.cxf.jaxws.helloworld.Server </mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
Before deploying the application, we will have to add two more files to your project.
Web.xml
<display-name>cxf</display-name> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> cxf </servlet-name> <url-pattern> /services/* </url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
Final pom.xml file:
It includes many more dependencies.
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>cxf-jaxws</artifactId> <version>1.0</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <profiles> <profile> <id>server</id> <build> <defaultGoal>test</defaultGoal> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass> com.example.cxf.jaxws.helloworld.Server </mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>client</id> <build> <defaultGoal>test</defaultGoal> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> <goals> <configuration> <mainClass> com.example.cxf.jaxws.helloworld.Client </mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-features-logging</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.3.0</version> </dependency> </dependencies> </project>
Running the HelloWorld Service
Now, you are ready to run the web app. In the command window, run the build script using the following command.
mvn clean install
mvn -Pserver
Console Message−
INFO: Setting the server’s publish address to be http://localhost:9090/HelloServerPort
Server is ready…
WebService using APACHE CXF:
Here’s, we will show you how to deploy the JAX-WS web services on Tomcat servlet container.
HelloWorld.java
import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; //Service Endpoint Interface @WebService @SOAPBinding(style = Style.RPC) public interface HelloWorld{ @WebMethod String getHelloWorldAsString(); }
HelloWorld1.java
import javax.jws.WebService; //Service Implementation Bean @WebService(endpointInterface = "com.demo.HelloWorld") public class HelloWorldImpl implements HelloWorld{ @Override public String getHelloWorldAsString() { return "Hello World JAX-WS"; } }
sun-jaxws.xml
Here we are creating a web service deployment descriptor, which is also known as JAX-WS RI deployment descriptor – sun-jaxws.xml.
sun-jaxws.xml
<endpoint name="HelloWorld" implementation="com.demo.HelloWorld1" url-pattern="/hello"/> </endpoints>
web.xml
<web-app> <listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener> <servlet> <servlet-name>hello</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>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <session-config> <session-timeout>120</session-timeout> </session-config> </web-app>
WAR Content
We are using Ant, Maven, or JAR command to build a WAR file to include everything inside.
File : build.xml
<project name="HelloWorldWS" default="dist" basedir="."> <description> Web Services build file </description> <!-- set global properties for this build --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/> <property name="webcontent" location="WebContent"/> <target name="init"> <!-- Create the time stamp --> <tstamp/> <mkdir dir="${build}"/> </target> <target name="compile" depends="init" description="compile the source " > <javac srcdir="${src}" destdir="${build}"/> </target> <target name="war" depends="compile" description="generate the distribution war" > <mkdir dir="${dist}/war"/> <copydir dest="${dist}/war/build/WEB-INF/" src="${webcontent}/WEB-INF/" /> <copydir dest="${dist}/war/build/WEB-INF/classes/" src="${build}" /> <jar jarfile="${dist}/war/HelloWorld-${DSTAMP}.war" basedir="${dist}/war/build/"/> </target> </project>
Deployment
You can access this URL: http://localhost:8081/HelloWorld/hello, to check the project has been deployed or not.