--- description: Follow these steps to set up a Java project with Aidbox --- # Use Aidbox with Java In this guide we will use the [HAPI FHIR Java client library](https://github.com/hapifhir/hapi-fhir) to create a Patient resource in Aidbox. ## Prerequisites - Run Aidbox locally by following the instructions in the [Run Aidbox locally](run-aidbox-locally.md) guide - Java - Maven ## Steps 1. Create a Client and an Access Policy. Navigate to [REST Console](http://localhost:8080/ui/console#/rest) in Aidbox UI and execute the following requests: ```json POST /fhir/Client content-type: application/json accept: application/json { "secret": "secret", "grant_types": [ "basic" ], "id": "basic", "resourceType": "Client" } ``` ```json POST /fhir/AccessPolicy content-type: application/json accept: application/json { "link": [ { "reference": "Client/basic" } ], "engine": "allow", "id": "basic-policy", "resourceType": "AccessPolicy" } ``` 2. Create pom.xml: ```xml 4.0.0 example.app aidbox-hapi-client 0.0.1-SNAPSHOT aidbox-hapi-client 8.2.0 ca.uhn.hapi.fhir hapi-fhir-structures-r4 ${hapi-fhir.version} ca.uhn.hapi.fhir hapi-fhir-client ${hapi-fhir.version} org.codehaus.mojo exec-maven-plugin 3.1.0 com.example.AidboxHapiClient ``` 3. Create **src/main/java/com/example/AidboxHapiClient.java**: ```java package com.example; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor; import ca.uhn.fhir.rest.api.MethodOutcome; import org.hl7.fhir.r4.model.*; import java.util.Date; /** * Example Java application that uses HAPI FHIR client to create a patient in Aidbox */ public class AidboxHapiClient { public static void main(String[] args) { try { FhirContext ctx = FhirContext.forR4(); IGenericClient client = ctx.newRestfulGenericClient("http://localhost:8080/fhir"); // Add basic authentication BasicAuthInterceptor authInterceptor = new BasicAuthInterceptor("basic", "secret"); client.registerInterceptor(authInterceptor); // Create a simple patient Patient patient = new Patient(); patient.addName().setFamily("Doe").addGiven("John"); patient.setBirthDate(new Date(90, 0, 15)); // January 15, 1990 patient.setGender(Enumerations.AdministrativeGender.MALE); System.out.println("Creating patient: " + patient.getNameFirstRep().getNameAsSingleString()); // Create the patient on the server MethodOutcome outcome = client.create() .resource(patient) .execute(); if (outcome.getCreated()) { System.out.println("Patient created successfully!"); System.out.println("Patient ID: " + outcome.getId().getIdPart()); } else { System.out.println("Patient creation may have failed or was updated"); } // Retrieve the created patient to verify Patient createdPatient = client.read() .resource(Patient.class) .withId(outcome.getId().getIdPart()) .execute(); System.out.println("Retrieved patient: " + createdPatient.getNameFirstRep().getNameAsSingleString()); System.out.println("Patient birth date: " + createdPatient.getBirthDate()); System.out.println("Patient gender: " + createdPatient.getGender()); } catch (Exception e) { System.err.println("Error creating patient: " + e.getMessage()); e.printStackTrace(); System.exit(1); } } } ``` 4. Build and run the application ```bash mvn clean compile exec:java ``` ## What happens? The application: - Connects to Aidbox running on http://localhost:8080 - Uses [basic authentication](../access-control/authentication/basic-http-authentication) with the basic client credentials - Creates a sample patient - Retrieves the created patient to verify the operation ## Next steps * Learn more about [Aidbox Access Control](../access-control/access-control.md) * See [Source code](https://github.com/Aidbox/examples/tree/main/developer-experience/aidbox-hapi-client)