A web service example to exchange HL7 messages using Apache CXF

This example shows you how to publish a web service using Apache CXF. There are two ways to develop a web service using JAX-WS front-end: Code-fist and contract-first. This example use the code-first approach, so we will first create a Java Interface and convert this into a web service component.

I have created a very simple web service with only two operations:

▪ Register a patient: This method gets the patient information from an HL7 message and adds the patient to a list.
▪ Get Patient by surname: This method finds a patient by id and returns the patient’s surname.

Configure your pom.xml

Have a look to the pom.xml. Note that we are using the maven-war-plugin so we need to define the packing as war. We have defined two profiles:

  • Server: This profile runs the class responsible of publishing the web service.
  • Client: This profile runs a client who calls the web service.

Model

In my model, I only have a Patient class with 3 attributes: name, surname and nhsNumber.

public class Patient {

private String name;

private String surname;

private int nhsNumber;

public Patient(String name, String surname, int nhsNumber) {
this.name = name;
this.surname = surname;
this.nhsNumber = nhsNumber;
}
//getters and setters omitted

Web Service Endpoint Interface and Implementation

As we said before, we are going to convert a Java interface into a web service component. For this purpose,  you need to add the @WebService annotation just above your interface definition. This is the only annotation that is mandatory to turn POJOs into web services. This annotation is part of the JAX-WS annotations and here you can find all the different options to configure your web service. By default, the name of the web service is going to be the same as the interface.

@WebService
public interface PatientService {

/**
* This method registers a new Patient
* @param message HL7 message which contains Patient details
*/
void registerPatient(String message);

/**
* Thei method obtains the surname for a given patient id
* @param nhsNumber Id of the Patient
* @return Surname of the Patient
*/
String getPatientSurnameByNHSNumber(int nhsNumber);
}

In the implementation you will need to add the “endpointInterface” to specify the interface and also the service name.

@WebService(endpointInterface = "com.hl7integration.ws.Server.PatientService",
serviceName = "PatientService")
public class PatientServiceImpl implements PatientService {

private static final Logger log = LoggerFactory.getLogger(PatientServiceImpl.class);

private List patients;

public PatientServiceImpl() {
this.patients = new ArrayList();
}

public void registerPatient(String message) {
int res = 0;
try{

PipeParser pipeParser = new PipeParser();
Message m = pipeParser.parse(message);
Terser terser = new Terser(m);

String surname = terser.get("/PID-5-1");
String name= terser.get("/PID-5-2");
int nhsNumber = Integer.parseInt(terser.get("/PID-2"));

Patient p = new Patient(name, surname, nhsNumber);
patients.add(p);

}catch(Exception e){
e.printStackTrace();
log.error("Cannot register a patient");
}
}

public String getPatientSurnameByNHSNumber(int nhsNumber) {

try{

for(Patient p : patients){
if(nhsNumber == p.getNhsNumber()){
return p.getSurname();
}
}

}catch(Exception e){
log.error("Cannot get patient's surname");
}
return null;
}
}

Publish the service

Finally, we need to publish the web service. The web service will be published in http://localhost:9999/patientService, and the wsdl is automatically generated and publish in the same address with “?wsdl” at the end (http://localhost:9999/patientService?wsdl).

public class Server {

protected Server() throws Exception {

System.out.println("Starting Server");
PatientServiceImpl implementor = new PatientServiceImpl();
String address = "http://localhost:9999/patientService";
Endpoint.publish(address, implementor);

}

public static void main(String args[]) throws Exception {
new Server();
System.out.println("Server ready...");

Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}

Create a Client

In order to test the web service we need to create a Client which will register a patient, and then retrieve his surname using the NHS number.

public class Client {

public static void main(String args[]) throws Exception {

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(PatientService.class);
factory.setAddress("http://localhost:9999/patientService");
PatientService ps = (PatientService) factory.create();

String message = "MSH|^~\\&|hl7Integration|hl7Integration|||||ADT^A31|||2.5|\r" +
"EVN|A31|20130617154644\r" +
"PID|1|46530651||407623|Wood^Patrick^^^MR||19700101|1|||High Street^^Oxford|";

//Call the service to register a patient
ps.registerPatient(message);

//Get the patient by nhs number
System.out.println("Patient Surname is: " + ps.getPatientSurnameByNHSNumber(46530651));

}
}

Run the server and Client

To run the server just try:
mvn -P server

Then to run the client try:
mvn -P client

Get the code
All my source code is hosted in github.

You may also like...

1 Response

  1. David says:

    I take pleasure in, cause I discovered exactly what I was having
    a look for. You’ve ended my four day long hunt! God Bless you man. Have a nice day.
    Bye

    site website good website site

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.