Skip to main content
Create Server

Create Server

This chapter builds a complete standalone POJO server, starts it with Maven, and checks the generated WSDL and a real SOAP request. The same structure is reused in the later chapters with different frontends.

Create the source files

src/main/java/com/example/pojo/GreetingService.java
src/main/java/com/example/pojo/PojoServer.java

Use the service class from the previous chapter and the server launcher shown below.

package com.example.pojo;

import org.apache.cxf.feature.LoggingFeature;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class PojoServer {

    public static void main(String[] args) {
        GreetingService service = new GreetingService();

        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
        factory.setServiceClass(GreetingService.class);
        factory.setServiceBean(service);
        factory.setAddress("http://localhost:8080/greeting");
        factory.getFeatures().add(new LoggingFeature());

        factory.create();
    }
}

Start the server

mvn -q compile exec:java -Dexec.mainClass=com.example.pojo.PojoServer

The process prints CXF startup lines and stays in the foreground. Keep this terminal open while testing the client in the next chapter.

Inspect the WSDL

curl -s "http://localhost:8080/greeting?wsdl" | head -40

Look for definitions, types, portType, binding, and service. If the page loads in a browser, the endpoint is running. A 404 usually means the address path or port is wrong; a connection refused error means the process is not running.

Send a SOAP request

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <greet xmlns="http://pojo.example.com/">
      <arg0>Ada</arg0>
    </greet>
  </soap:Body>
</soap:Envelope>
curl -s -X POST http://localhost:8080/greeting \
  -H "Content-Type: text/xml; charset=utf-8" \
  -H "SOAPAction: \"\"" \
  --data-binary @request.xml

The response contains a SOAP envelope with the greeting. If the namespace or element name does not match the WSDL, CXF returns a fault instead of calling the method.

Add a custom endpoint path

Multiple services can share one server when each has a distinct address. Start several factories on different paths, or deploy them into a servlet container in the JAX-WS chapter.

factory.setAddress("http://localhost:8080/services/greeting");

Server checklist

  • Log the WSDL URL on startup so testers do not guess the path.
  • Set an explicit target namespace before publishing a contract to other teams.
  • Use LoggingFeature during development and disable verbose payload logging in production.
  • Handle shutdown so the port is released when the process stops.