July 14, 2023Marcus Hellberg

Integrating Pinecone Vector Database into a Spring Boot Application

Dive into the process of integrating the Pinecone Vector database with a Spring Boot application using Java in this second installment of the Building an AI chatbot in Java series.


The article assumes you have a Spring Boot application. The example project is a Spring Boot application with a React front end created with Hilla. To build a new app, follow the instructions in the Hilla documentation.

To use reactive data types, add this dependency to your pom.xml file:


Acquiring a Pinecone API Key

To create a new API key, visit the Pinecone console. Store the key in an environment variable PINECONE_API_KEY. Also, save your Pinecone index URL as PINECONE_API_URL. Depending on your needs, environment variables can be stored in your IDE or system-wide.

Setting Up a Service Class

All Pinecone-related code will be in a Spring service class accessible from other parts of our application. Create a new class, PineconService.java:

public class PineconeService {

    private String PINECONE_API_KEY;

    private String PINECONE_API_URL;

Configuring Spring WebClient for Pinecone API

Spring WebClient will be used for making calls to Pinecone REST services. Initialize the client by adding the following to PineconeService.java:

private WebClient webClient;

void init() {
    this.webClient = WebClient.builder()
            .defaultHeader("Api-Key", PINECONE_API_KEY)
            .defaultHeader("Accept", MediaType.APPLICATION_JSON_VALUE)
            .defaultHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)

Querying Pinecone

Now that the client is configured, you can call the Pinecone API to query records. Add the following method to PineconeService.java:

public Mono<List<String>> findSimilarDocuments(List<Double> embedding, int maxResults, String namespace) {

    var body = Map.of(
            "vector", embedding,
            "topK", maxResults,
            "includeMetadata", true,
            "namespace", namespace

    return this.webClient.post()
            .flatMapIterable(matches -> matches)
            .map(match -> {
                if (match.getMetadata() != null && match.getMetadata().containsKey("text")) {
                    return match.getMetadata().get("text");
                } else {
                    return "";
            .filter(doc -> !doc.isBlank())

The method takes an embedding vector (obtained by calling the OpenAI embeddings API in the previous article), the number of similar documents you want, and the namespace for the search. The Java classes for the response can be found in the project's GitHub repository.

The method returns the source texts from the metadata of the matching vectors.

Completed Application Source Code

The completed application's source code is available on GitHub.

What's Next?

As part of the Building an AI chatbot in Java series, you now have services for calling both OpenAI and Pinecone. The following article will build a service that uses the Pinecone service to fetch articles to include as context for the ChatGPT prompt.

Marcus Hellberg

Marcus Hellberg

Marcus is the VP of Developer Relations at Vaadin. His daily work includes everything from writing blogs and tech demos to attending events and giving presentations on all things Vaadin and web-related.

© 2024 Vaadin. All rights reserved