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) { = 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;

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 javax.servlet.ServletConfig;
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;

public void loadBus(ServletConfig servletConfig) {
    Bus bus = getBus
try {
ServerSocket sock = new ServerSocket();
        InetSocketAddress s =
new InetSocketAddress(InetAddress.getLocalHost(), 0);
int port = sock.getLocalPort();
        String address =
"http://localhost:" + port + "/ChangeStudent";
("Publishing on " + address);
(address, new ChangeStudentDetailsImpl());
} catch (Exception e) {
// TODO Auto-generated catch block

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 *