CXF WS-Discovery Example

Create a Student Object

package com.student;

public class Student {
 
private String name;
 
public String getName() {
   
return name;
 
}
 
public void setName(String name) {
   
this.name = name;
 
}
}

Create a Service Interface

This service interface will defines which methods of web service, to be invoked by the client

package com.student;

import javax.jws.WebService;

@WebService
public interface ChangeStudentDetails {
 
Student changeName(Student student);
}

Implement the Service Interface

Here we implement the service interface created on the previous step

package com.student;

import javax.jws.WebService;

@WebService(endpointInterface = "com.student.ChangeStudentDetails")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {
   
public Student changeName(Student student) {
     
student.setName("Hello "+student.getName());
     
return student;
   
}
}

Create a SimpleCXFNonSpringServlet

Here we are using SimpleCXFNonSpringServlet class in order to add service implementation class(ChangeStudentDetailsImpl)

We are not hard coding the port for deploying the service, but using random port. Deployment is done dynamically using CXF WS-Discovery service, so port may change on each deployment.

package com.student;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;

import javax.servlet.ServletConfig;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;

public class SimpleCXFNonSpringServlet extends CXFNonSpringServlet {

 
private static final long serialVersionUID = 1L;

 
@Override
 
public void loadBus(ServletConfig servletConfig) {
   
super.loadBus(servletConfig);
    Bus bus = getBus
();
    BusFactory.setDefaultBus
(bus);
   
   
try {
       
ServerSocket sock = new ServerSocket();
        InetSocketAddress s =
new InetSocketAddress(InetAddress.getLocalHost(), 0);
        sock.bind
(s);
       
int port = sock.getLocalPort();
        sock.close
();
        String address =
"http://localhost:" + port + "/ChangeStudent";
        System.out.println
("Publishing on " + address);
       
        Endpoint.publish
(address, new ChangeStudentDetailsImpl());
   
} catch (Exception e) {
       
// TODO Auto-generated catch block
       
e.printStackTrace();
   
}
  }
}





Previous 1 2 3 Next





5 Responses to "CXF WS-Discovery Example"
  1. Anton 2014-06-03 19:02:57.0
  1. admin 2014-06-04 19:02:57.0
  1. Carson 2014-06-05 19:02:57.0
  1. Carson 2014-06-06 19:02:57.0
  1. Carson 2014-06-07 19:02:57.0

Your email address will not be published. Required fields are marked *