A new programming paradigm, when Spring Boot meets OpenAI
In 2023, AI technology has become a hot topic and has had a huge impact on various industries, especially in the programming field. People are increasingly aware of the importance of AI technology, and the Spring community is no exception.
With the continuous advancement of GenAI (General Artificial Intelligence) technology, it has become crucial and urgent to simplify the creation of applications with AI functions. Against this background, "Spring AI" emerged, aiming to simplify the process of developing AI functional applications, making it simple and intuitive and avoiding unnecessary complexity. Through "Spring AI", developers can more easily build applications with AI functions, making them easier to use and operate. This not only helps improve development efficiency, but also accelerates the popularization and application of AI technology. In short, "Spring AI" brings new possibilities to the development of AI applications, providing developers with simpler and more intuitive tools and frameworks.
This article will briefly introduce the Spring AI framework and some engineering tips for using the framework. Developers can use these tips to better structure prompt information and fully utilize the capabilities of Spring AI.
1 Introduction to Spring AI
Spring AI is created and written by M K Pavan Kumar
Spring AI is a tool designed to simplify AI applications Developed project inspired by the Python projects LangChain and LlamaIndex. However, Spring AI is not a simple copy. Its core idea is to open generative AI applications to users of various programming languages, not just Python language enthusiasts. This means developers can build AI applications using a language they are familiar with without having to learn the Python language. With Spring AI, developers can more easily harness the power of AI to solve a variety of problems, regardless of which programming language they use. This will facilitate broader AI application development and provide developers with more flexibility and choice.
The core goal of Spring AI is to provide the basic building blocks for building AI-driven applications. These building blocks are highly flexible and components can be easily swapped with virtually no modifications to the code. One example is that Spring AI introduces a component called the ChatClient interface, which is compatible with OpenAI and Azure OpenAI technologies. This allows developers to switch between different AI service providers without changing the code, making development and integration more convenient.
At its core, Spring AI provides reliable building blocks for developing artificial intelligence-based applications. The elasticity of these modules enables smooth swapping of components without requiring extensive modifications to the coding. One example is Spring AI's introduction of the ChatClient interface, which is compatible with OpenAI and Azure OpenAI, allowing developers to easily talk to both platforms. This compatibility allows developers to choose the appropriate platform based on actual needs without having to rewrite code. With Spring AI, developers can build AI-driven applications more efficiently.
Spring AI goes beyond basic building blocks and focuses on providing more advanced solutions. For example, it can support typical scenarios such as "questions and answers about one's own documents" or "interactive chat using documents". As application needs grow, Spring AI plans to work closely with other components of the Spring ecosystem such as Spring Integration, Spring Batch and Spring Data to meet more complex business needs.
2 Create a Spring Boot project and write an OpenAI controller example
First generate the Spring Boot project in the IDE and keep the following content in the application.properties file:
spring.ai.openai.api-key=<YOUR\_OPENAI\_API\_KEY>
Below Write a controller named OpenAIController.java:
package com.vas.springai.controller;import org.springframework.ai.client.AiClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/api/v1")public class OpenAIController {private final AiClient aiClient;public OpenAIController(AiClient aiClient) {this.aiClient = aiClient;}}
3 Use the Prompt class to build prompt information
The prompt class is a structured holder of a sequence of message objects, each message represents a prompt a part of. These messages have different roles and purposes in the prompt, and their content varies. Includes user questions, AI-generated responses, relevant contextual details, and more. This setup facilitates complex and sophisticated human-computer interactions since the prompt consists of multiple messages with specific functions.
@GetMapping("/completion")public String completion(@RequestParam(value = "message") String message){return this.aiClient.generate(message);}
However, aiClient's generate method does not only accept plain text as a parameter, it can also accept objects of the Prompt class as parameters, as shown below. Now, this method returns an instance of type AiResponse, not simple text.
@GetMapping("/completion")public AiResponse completion(@RequestParam(value = "message") String message){ PromptTemplate promptTemplate = new PromptTemplate("translate the given english sentence sentence into french {query}"); Prompt prompt = promptTemplate.create(Map.of("query", message)); return this.aiClient.generate(prompt);}
In addition, the Prompt class also provides an overloaded constructor that can accept a sequence of Message type instances with different roles and intentions as parameters. This can better organize and manage prompt information and facilitate subsequent processing and use. Below is a sample code showing how to use this overloaded constructor to merge everything.
package com.vas.springai.controller;import org.springframework.ai.client.AiClient;import org.springframework.ai.client.Generation;import org.springframework.ai.prompt.Prompt;import org.springframework.ai.prompt.PromptTemplate;import org.springframework.ai.prompt.SystemPromptTemplate;import org.springframework.ai.prompt.messages.Message;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.Map;@RestController@RequestMapping("/api/v1")public class OpenAIController {private final AiClient aiClient;public OpenAIController(AiClient aiClient) {this.aiClient = aiClient;}@GetMapping("/completion")public List<Generation> completion(@RequestParam(value = "message") String message) {String systemPrompt = """You are a helpful AI assistant that helps people translate given text from english to french.Your name is TranslateProYou should reply to the user's request with your name and also in the style of a professional.""";SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemPrompt);Message systemMessage = systemPromptTemplate.createMessage();PromptTemplate promptTemplate = new PromptTemplate("translate the given english sentence sentence into french {query}");Message userMessage = promptTemplate.createMessage(Map.of("query", message));Prompt prompt = new Prompt(List.of(systemMessage, userMessage));return this.aiClient.generate(prompt).getGenerations();}}
4 Testing the application
You can use any open tool available on the market to test the application, such as postman, insomnia, Httpie, etc.
picture
The above is the detailed content of A new programming paradigm, when Spring Boot meets OpenAI. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The following factors should be considered when choosing a bulk trading platform: 1. Liquidity: Priority is given to platforms with an average daily trading volume of more than US$5 billion. 2. Compliance: Check whether the platform holds licenses such as FinCEN in the United States, MiCA in the European Union. 3. Security: Cold wallet storage ratio and insurance mechanism are key indicators. 4. Service capability: Whether to provide exclusive account managers and customized transaction tools.

Provides a variety of complex trading tools and market analysis. It covers more than 100 countries, has an average daily derivative trading volume of over US$30 billion, supports more than 300 trading pairs and 200 times leverage, has strong technical strength, a huge global user base, provides professional trading platforms, secure storage solutions and rich trading pairs.

The top ten secure digital currency exchanges in 2025 are: 1. Binance, 2. OKX, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. Bitfinex, 8. KuCoin, 9. Bybit, 10. Bitstamp. These platforms adopt multi-level security measures, including separation of hot and cold wallets, multi-signature technology, and a 24/7 monitoring system to ensure the safety of user funds.

Common stablecoins are: 1. Tether, issued by Tether, pegged to the US dollar, widely used but transparency has been questioned; 2. US dollar, issued by Circle and Coinbase, with high transparency and favored by institutions; 3. DAI, issued by MakerDAO, decentralized, and popular in the DeFi field; 4. Binance Dollar (BUSD), cooperated by Binance and Paxos, and performed excellent in transactions and payments; 5. TrustTo

As of 2025, the number of stablecoin exchanges is about 1,000. 1. Stable coins supported by fiat currencies include USDT, USDC, etc. 2. Cryptocurrency-backed stablecoins such as DAI and sUSD. 3. Algorithm stablecoins such as TerraUSD. 4. There are also hybrid stablecoins.

As of April 2025, seven cryptocurrency projects are considered to have significant growth potential: 1. Filecoin (FIL) achieves rapid development through distributed storage networks; 2. Aptos (APT) attracts DApp developers with high-performance Layer 1 public chains; 3. Polygon (MATIC) improves Ethereum network performance; 4. Chainlink (LINK) serves as a decentralized oracle network to meet smart contract needs; 5. Avalanche (AVAX) trades quickly and

Choosing a reliable exchange is crucial. The top ten exchanges such as Binance, OKX, and Gate.io have their own characteristics. New apps such as CoinGecko and Crypto.com are also worth paying attention to.

DLC coins are blockchain-based cryptocurrencies that aim to provide an efficient and secure trading platform, support smart contracts and cross-chain technologies, and are suitable for the financial and payment fields.
