Why does it matter?
Imagine your users leave comments, reports, reviews, or training feedback. Each of these texts carries emotions – you just need to read them. And that’s where sentiment analysis comes in. One call to the microservice is all it takes to get a simple, clear answer: positive, neutral, or negative.
It gives you instant insight into customer moods and lets you respond faster – address frustration, reward loyalty, improve service quality.
And most importantly – you’ll do all of this without using Python, NLP libraries, or local model training. All you need is Java, Spring Boot, and the OpenAI API. In our example, we’ll start by manually entering opinions to test the service. In the next part, we’ll show how to automate sentiment extraction from the web.
What you need:
- Java 17+
- Spring Boot (e.g., via Spring Initializr)
- API key from OpenAI platform
- Netbeans 24
- Insomnia
1. Project configuration
First, we need a simple Spring Boot app skeleton – our microservice. We’ll configure dependencies, set up a REST controller, and enable communication with OpenAI via API. No magic here – just specific steps you can repeat in your own project.
Go to https://start.spring.io/ and generate a new project with Java 17 and Spring Boot.
In NetBeans, add the following dependencies to the build.gradle
file:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
In application.property
:
spring.application.name=ai-demo
openai.api-key=secret_key
openai.model=gpt-3.5-turbo
2. Service: sentiment analysis using OpenAI
In this part, we’ll create a service responsible for communication with the GPT-3.5 model. The goal is to send the user’s text as a prompt and receive a single concise response: positive, neutral, or negative. We’ll use the WebClient library for HTTP requests in Spring Boot.
package com.itsilesia.ai.ai_demo;
import jakarta.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.stereotype.Service;
@Service
public class OpenAiService {
@Value("${openai.api-key}")
private String apiKey;
@Value("${openai.model}")
private String model;
private WebClient webClient;
@PostConstruct
public void init(){
webClient = WebClient.builder()
.baseUrl("https://api.openai.com/v1")
.defaultHeader("Authorization", "Bearer " + apiKey)
.defaultHeader("Content-Type", "application/json")
.build();
}
public String analyzeSentiment(String text) {
String prompt = "Analyze the sentiment of the following text. Respond with one word: Positive, Neutral or Negative.\nText: " + text;
Map<String, Object> message = Map.of(
"role", "user",
"content", prompt
);
Map<String, Object> body = Map.of(
"model", model,
"messages", List.of(message)
);
return webClient.post()
.uri("/chat/completions")
.bodyValue(body)
.retrieve()
.bodyToMono(Map.class)
.map(resp -> ((Map)((List)resp.get("choices")).get(0)).get("message").toString())
.block();
}
}
3. REST controller for querying sentiment results
In this part, we’ll create a REST controller to expose our sentiment analysis service to the outside world. It will take a text payload, pass it to the GPT-3.5 model via our service, and return the sentiment result.
package com.itsilesia.ai.ai_demo;
import java.util.Map;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/sentiment")
public class SentimentController {
private final OpenAiService openAiService;
public SentimentController(OpenAiService openAiService) {
this.openAiService = openAiService;
}
@PostMapping
public ResponseEntity analyze(@RequestBody Map<String, String> payload) {
String text = payload.get("text");
String result = openAiService.analyzeSentiment(text);
return ResponseEntity.ok(result);
}
}
4. Result
Open Insomnia and send a request to http://localhost:8080/sentiment/
Alternatively, in the console:
Summary
We built a working microservice that can determine in seconds whether a given text is positive, neutral, or negative. We used Java, Spring Boot, and the OpenAI API – with no external NLP libraries or model training required. It works independently of the input language – yes, even in Polish – and can be easily integrated into contact forms, product reviews, or ticketing systems.
This is a perfect solution for developers who want to quickly enhance their apps with opinion analysis – without learning Python or diving into the world of natural language processing. Simple, effective, and fully production-ready – just the way we like it in the Java world.
In short: Java + AI = it works. Simple, fast, and effective.
What’s next?
- In the next part, we’ll connect it to the internet and analyze real-world reviews
- Want the full source code? Drop a comment saying “CODE” 👇