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.

[java]
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
[/java]

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.
[java]
@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);
}
[/java]

In the implementation you will need to add the “endpointInterface” to specify the interface and also the service name.
[java]
@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;
}
}
[/java]
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).
[java]
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);
}
}
[/java]

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.
[java]
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));

}
}
[/java]

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...

35 Responses

  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

  2. Google UltraFan:) says:

    Wiedza i kompetencja płynie z twoich postów inspirują i zmuszają do refleksji. Dzięki Ci za tak bogaty wkład w społeczność blogową. Trzymaj tak dalej Cześć!

    https://xmc.pl

  3. Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

  4. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

  5. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

  6. brTyTTXHRlSbrpecQXUWEU says:

    kfoBTVZrhOsHadaEy

  7. Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://www.binance.com/register?ref=IHJUI7TF

  8. Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://accounts.binance.info/es-AR/register/person?ref=UT2YTZSU

  9. binance kod says:

    Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

  10. Tristan says:

    人生において、充実した時間を見つけることはとても大切なことです。
    そうした理由から、自分に合ったアイテムやサービスを選ぶことが重要になります。
    最近では、プライベートを尊重する商品やサービスが広まっています。
    このサイトでは、利用者の満足度を第一に考え、役立つ情報を提供しています。
    素材の比較など、抑えておきたい部分を丁寧にまとめています。
    サポート体制の充実がしっかりしているため、不安な方でも安心して利用できます。
    難しい知識であっても、易しく説明しているのが特徴です。
    日常での使い方をイメージしながら読むこ

    Here is my blog: リアルラブドール

  11. Shella says:

    リアルドールを選ぶ上で最も重要な要素の一つが素材の種類です。
    主に使用されるシリコンとTPEは、それぞれ特徴が大きく異なり、自分の目的によって最適な選択が変わります。
    シリコン製のドールは、耐久性が高く、汚れが付きにくくである特徴があります。
    長期間使用する予定の方にとって、非常に適した選択肢といえます。
    対してTPEは、柔らかさに優れ、コストパフォーマンスが高いため初心者に人気です。
    種類によっては、お手入れ方法や注意点が異なるため、事前に理解しておくことが重要です。
    アレルギーリスクや衛生面についても、素材ごとに比較検証ししています。
    使用頻度や保管環境に合わせて、自分に合った種類を選ぶことが大切です。
    初心者であっても、内容を参考にすれば、明確な判断ができるようになります。
    素材の特徴を正しく知ることで、満足できる購入ができるようになります。
    最適な素材を見つけることが、長く楽しむための秘訣です。

    Here is my web site; ラブドール

  12. Mireya says:

    tischspiele online um geld spielen

    Also visit my webpage :: Alle Spielbanken In Deutschland

  13. Hester says:

    forhandler nær mig

    Also visit my web-site: Alle Danske Bettingselskaber (https://En.Ewaosinska.Com)

  14. Jesenia says:

    how to bet on horse racing odds​

    Here is my blog Greyhound prediction​

  15. Kristal says:

    neue online wettanbieter

    my homepage – Basketball Wetten

  16. Jaimie says:

    wetten deutschland spanien

    Also visit my site basketball wm wett tipps (Randy)

  17. Caroline says:

    betting company odds comparison

  18. Son says:

    wett tipps ai erfahrungen

    Also visit my web page :: basketball wetten

  19. Conrad says:

    apuestas alcaraz hoy (agroia.Es)
    partidos mundial

  20. Major says:

    rec sports apostas donde apostar online argentina (Serena)

  21. Quinn says:

    888 poker login united states, top casino slots uk
    and new zealandn casino no deposit bonus codes, or casoola casino uk login

    Also visit my blog post roulette pour flight case [Alexandria]

  22. German says:

    casinos que pagan mas rapido

    Here is my site – máquinas para ganar Dinero en casa Casino online

  23. Felipa says:

    wettquoten esc deutschland

    Feel free to surf to my webpage … sichere basketball wetten

  24. Grant says:

    o que são apostas esportivas, Louie, que é aposta multipla

  25. Marcelino says:

    live sportwetten Basketball wm wett tipps [https://vuichard-sa.Ch/]

  26. Toby says:

    alle buchmacher

    Also visit my page :: basketball tipps wetten (Vickey)

  27. Leonie says:

    nye casino vejle

    my homepage :: bingo Uden rofus

  28. Erik says:

    sportwetten deutscher meister

    Feel free to visit my web-site: basketball pro a wetten

  29. Noella says:

    casino en carmelo españa

    Look at my web-site: lista de casinos online

  30. Andreas says:

    apostar em ténis

    Feel free to visit my homepage … apostas esportivas online gratis
    (Arianne)

  31. Jaimie says:

    online casino mit telefon auszahlung (Danae) casino 25
    euro apple pay

Leave a Reply

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