Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

WebService using Apache CXF/Tomcat

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 −


   
      server
      
         test
         
            
               org.codehaus.mojo
               exec-maven-plugin
               1.6.0
               
                  
                     test
                     
                        java
                     
                     
                        
                           com.demo.cxf.jaxws.helloworld.Server
                        
                     
                  
               
            
         
      
   

Before deploying the application, we will have to add two more files to your project.

Web.xml

  cxf
   
      Apache CXF Endpoint
      cxf
      cxf
      
         org.apache.cxf.transport.servlet.CXFServlet
      
      
         1
      
   
   
      
         cxf
      
      
         /services/*
      
   
   
      60
   

Final pom.xml file:

It includes many more dependencies.

4.0.0
   com.example
   cxf-jaxws
   1.0
   jar
   
      UTF-8
      1.8
      1.8
   
   
      
         server
         
            test
            
               
                  org.codehaus.mojo
                  exec-maven-plugin
                  1.6.0
                  
                     
                        test
                        
                           java
                        
                        
                           
                              com.example.cxf.jaxws.helloworld.Server
                           
                        
                     
                  
               
            
         
      
      
         client
         
            test
            
               
                  org.codehaus.mojo
                  exec-maven-plugin
                  
                     
                        test
                        
                           java
                        
                        
                           
                              com.example.cxf.jaxws.helloworld.Client
                           
                        
                     
                  
               
            
         
      
   
   
      
         org.apache.cxf
         cxf-rt-frontend-jaxws
         3.3.0
      
      
         org.apache.cxf
         cxf-rt-transports-http
         3.3.0
      
      
         org.apache.cxf
         cxf-rt-features-logging
         3.3.0
      
      
         org.apache.cxf
         cxf-rt-transports-http-jetty
         3.3.0
      
   

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

web.xml


    
        
                com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        
    
    
        hello
        
         com.sun.xml.ws.transport.http.servlet.WSServlet
        
        1
    
    
        hello
        /hello
    
    
        120
    

WAR Content

We are using Ant, Maven, or JAR command to build a WAR file to include everything inside. 
File : build.xml


    
        Web Services build file
    
  
  
  
  
  
  
        
        
           
  
  
        
  
  
   
   
   
 
  

Deployment

You can access this URL: http://localhost:8081/HelloWorld/hello, to check the project has been deployed or not.

The post Webservice using Apache CXF/Tomcat appeared first on H2kinfosys Blog.



This post first appeared on It Online Training Courses, please read the originial post: here

Share the post

WebService using Apache CXF/Tomcat

×

Subscribe to It Online Training Courses

Get updates delivered right to your inbox!

Thank you for your subscription

×