DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Upcoming DZone Events
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Improving DAG Failure Detection in Airflow Using AI Techniques

Trending

  • Bringing Intelligence Closer to the Source: Why Real-Time Processing is the Heart of Edge AI
  • Introduction to Retrieval Augmented Generation (RAG)
  • LLM Integration in Enterprise Applications: A Practical Guide
  • DevOps and Platform Engineering Readiness Checklist: Everything Needed for a Scalable, Secure, High-Velocity Delivery Platform
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. AI Agents in Java: Architecting Intelligent Health Data Systems

AI Agents in Java: Architecting Intelligent Health Data Systems

This article explores designing AI agents in Java for intelligent healthcare systems using event-driven architectures for secure, scalable data processing.

By 
Ramya vani Rayala user avatar
Ramya vani Rayala
·
May. 20, 26 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
1.6K Views

Join the DZone community and get the full member experience.

Join For Free

Executive Summary

Modern health data analytics increasingly leverage AI agent software components that process information and make decisions, often using large language models (LLMs) or machine learning models. In Java, you can build agentic systems using libraries like DJL (Deep Java Library), Spring AI, or by integrating LLM APIs. This document includes Maven setup, minimal Spring Boot code (controllers and services), a simple agent example, diagrams, and a comparison of different agent approaches.

Flowchart

Flowchart image


Maven Dependencies

Define the necessary dependencies in pom.xml (Spring Web, Validation, DJL, OpenAI SDK, etc.):

XML
 
<dependencies>

  <!-- Spring Boot Web for API -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <!-- Validation (optional) -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
  </dependency>

  <!-- OpenAI Java SDK (LLM API) -->
  <dependency>
    <groupId>com.openai</groupId>
    <artifactId>openai-java</artifactId>
    <version>4.0.0</version>
  </dependency>

  <!-- DJL for local ML inference -->
  <dependency>
    <groupId>ai.djl</groupId>
    <artifactId>api</artifactId>
    <version>0.36.0</version>
  </dependency>

  <dependency>
    <groupId>ai.djl.mxnet</groupId>
    <artifactId>mxnet-engine</artifactId>
    <version>0.36.0</version>
  </dependency>

  <!-- (Optional) Spring AI -->
  <dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>spring-boot-starter-ai</artifactId>
    <version>0.0.1</version>
  </dependency>

</dependencies>


This setup assumes you will use the OpenAI Java SDK and DJL. Replace the spring-boot-starter-ai version with the latest as needed.

1. Domain Model & Configuration

Define simple data classes for health analysis requests and responses in com.example.health:

Java
 
package com.example.health;

public record VitalSigns(
    double temperature,
    double bloodPressure,
    int heartRate
) {}


This example record holds patient vitals. You can also add a Spring @Configuration if needed, for example, to configure the DJL engine:

Java
 
package com.example.config;

import org.springframework.context.annotation.Configuration;
import ai.djl.Engine;

@Configuration
public class DjLConfig {

    public DjLConfig() {
        Engine.getEngine("MXNet");
    }
}


No special configuration is required for OpenAI; it reads API keys from the OPENAI_API_KEY environment variable.

2. Service Layer / Agent Component

Implement a service that acts as your “agent.” It can use a local DJL model or call an external LLM API:

Java
 
package com.example.agent;

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
import org.springframework.stereotype.Service;

@Service
public class HealthAgent {

    private final OpenAIClient openAiClient;

    public HealthAgent() {
        this.openAiClient = OpenAIOkHttpClient.fromEnv();
    }

    public String analyzeVitals(String patientId, VitalSigns vitals) {
        String prompt = String.format(
            "Patient %s has temperature %.1f°C, blood pressure %.0f/%d, heart rate %d. Suggest the next diagnostic step.",
            patientId, vitals.temperature(), vitals.bloodPressure(), vitals.bloodPressure(), vitals.heartRate()
        );

        ResponseCreateParams params = ResponseCreateParams.builder()
                .model("health")
                .input(prompt)
                .build();

        Response response = openAiClient.responses().create(params);
        return response.outputText();
    }
}


This HealthAgent service builds a prompt from VitalSigns and uses the OpenAI Java SDK to call the LLM.

3. REST Controller

Expose an HTTP API to trigger the agent:

Java
 
package com.example.api;

import com.example.agent.HealthAgent;
import com.example.health.VitalSigns;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/health")
public class HealthController {

    private final HealthAgent agent;

    public HealthController(HealthAgent agent) {
        this.agent = agent;
    }

    @PostMapping("/analyze")
    public ResponseEntity<String> analyze(
            @RequestParam String patientId,
            @RequestBody VitalSigns vitals) {

        String result = agent.analyzeVitals(patientId, vitals);
        return ResponseEntity.ok(result);
    }
}


This controller maps POST /api/health/analyze?patientId=XYZ with a JSON body containing VitalSigns. It delegates to the HealthAgent and returns the response.

4. Agent Patterns and Architecture

AI “agents” can follow different patterns. Spring AI documentation distinguishes workflows from fully agentic systems that act autonomously. In healthcare, predictable workflows are often preferred for safety, although LLM-based agents can be dynamic.

Common agent patterns include:

  • Chaining: Multi-step reasoning
  • Parallelization: Running tasks in parallel and combining results
  • Routing: Directing inputs to specialized prompts or tools
  • Looping: Iterating until a goal is achieved

Spring AI provides abstractions to implement these patterns. For example, a chain workflow may sequentially call ChatClient.prompt(...).call() while passing outputs between steps.

The example above is a single-step LLM call, but the architecture can be expanded:

Clients → Spring Boot API → Agent (Spring AI or custom logic) → LLM/model → API response

5. Table: Agent Approaches

Approach

Pros

Cons

Use Cases

Custom Java Logic 

Fully controllable code no external calls

Limited intelligence no learning or language understanding

Simple rule-based analysis strict data privacy requirements

DJL 

Runs on JVM, GPU support  full control of model and data

Heavy dependency must train/download models resource-intensive

On-premises analytics private data processing  when a fixed ML model suffices

LLM API (OpenAI, etc.)

State-of-art language understanding managed by provider

Latency, cost, and data privacy concerns requires API key

NLP-heavy tasks prototyping and research

Spring AI Patterns

High-level workflows (chain, parallel, routing) built-in integrates with Spring

Underlying calls are still to models/APIs complexity overhead

Enterprise applications needing structured LLM agents, combining LLM with Spring ecosystem


6. Implementation Timeline

  • 2026-02-01: Project kickoff (set up Spring Boot)
  • 2026-02-03: Define domain models (VitalSigns, etc.)
  • 2026-02-05: Configure dependencies (DJL, OpenAI SDK)
  • 2026-02-07: Implement HealthAgent service (LLM call)
  • 2026-02-10: Add REST controller and test endpoint
  • 2026-02-12: Optional: integrate DJL model (local inference)
  • 2026-02-14: Add error handling and validation
  • 2026-02-15: Final testing and local deployment

7. Running Locally

Set your API key:
Ensure OPENAI_API_KEY is set as an environment variable.

Build and run (using Maven):

Plain Text
 
mvn spring-boot:run


By default, the app runs on http://localhost:8080.

Test the API:

Plain Text
 
curl -X POST "http://localhost:8080/api/health/analyze?patientId=123" \
     -H "Content-Type: application/json" \
     -d '{"temperature": 38.5, "bloodPressure": 130, "heartRate": 95}'


You should see the AI agent’s response as plain text.

8. Assumptions

  • Java Version: Java 17 (no license restrictions assumed)
  • External Services: OpenAI or other LLM APIs require network access and valid API keys
  • Data Privacy: Health data must be handled securely (e.g., encryption, HIPAA compliance)
  • Machine Learning: DJL uses MXNet by default; GPU acceleration requires proper setup
  • Architecture: Kafka, WebSockets, or databases are omitted for brevity 

Conclusion

This document outlines how to integrate AI agents into a Spring Boot Java application. Key takeaways:

  • Project Setup: Use Spring Boot for REST APIs with ML/LLM dependencies
  • Agent Logic: Implement services that call LLM APIs or local ML models
  • Patterns: Use structured agent patterns for complex workflows
  • Trade-offs: Choose between custom logic, local ML, or LLM APIs based on requirements
AI Health (Apple) Java (programming language) systems

Opinions expressed by DZone contributors are their own.

Related

  • Upcoming DZone Events
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Improving DAG Failure Detection in Airflow Using AI Techniques

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook