diff --git a/spring-ai/pom.xml b/spring-ai/pom.xml
index 52eba30508..572941358b 100644
--- a/spring-ai/pom.xml
+++ b/spring-ai/pom.xml
@@ -9,9 +9,9 @@
com.baeldung
- parent-boot-2
+ parent-boot-3
0.0.1-SNAPSHOT
- ../parent-boot-2
+ ../parent-boot-3
diff --git a/spring-ai/src/main/java/com/baeldung/spring/ai/web/ExceptionTranslator.java b/spring-ai/src/main/java/com/baeldung/spring/ai/web/ExceptionTranslator.java
new file mode 100644
index 0000000000..98c6a35e94
--- /dev/null
+++ b/spring-ai/src/main/java/com/baeldung/spring/ai/web/ExceptionTranslator.java
@@ -0,0 +1,19 @@
+package com.baeldung.spring.ai.web;
+
+import com.theokanning.openai.OpenAiHttpException;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ProblemDetail;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
+
+@RestControllerAdvice
+public class ExceptionTranslator extends ResponseEntityExceptionHandler {
+
+ @ExceptionHandler(OpenAiHttpException.class)
+ ProblemDetail handleOpenAiHttpException(OpenAiHttpException ex){
+ ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getMessage());
+ problemDetail.setTitle("Open AI client raised exception");
+ return problemDetail;
+ }
+}
diff --git a/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java b/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java
index b6e510c65b..7c612e5fee 100644
--- a/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java
+++ b/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java
@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("ai")
public class PoetryController {
- public static final String WRITE_ME_HAIKU_ABOUT_CAT = "Write me Haiku about cat";
+ public static final String WRITE_ME_HAIKU_ABOUT_CAT = "Write me Haiku about cat, haiku should contain word cat";
private final AiClient aiClient;
@Autowired
diff --git a/spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerIntegrationTest.java b/spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerIntegrationTest.java
new file mode 100644
index 0000000000..782eb6aa86
--- /dev/null
+++ b/spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerIntegrationTest.java
@@ -0,0 +1,38 @@
+package com.baeldung.spring.ai.web;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.ai.client.AiClient;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.web.servlet.MockMvc;
+
+import static org.hamcrest.Matchers.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+
+@AutoConfigureMockMvc
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class PoetryControllerIntegrationTest {
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @Autowired
+ private ObjectMapper objectMapper;
+
+ @Autowired
+ private AiClient aiClient;
+
+ @Test
+ public void getCatHaikuTest() throws Exception {
+ mockMvc.perform(get("/ai/cathaiku"))
+ .andExpect(status().isOk())
+ .andExpect(content().string(containsStringIgnoringCase("cat")));
+ }
+}
diff --git a/spring-ai/src/test/resources/application.yml b/spring-ai/src/test/resources/application.yml
new file mode 100644
index 0000000000..dfc8294e45
--- /dev/null
+++ b/spring-ai/src/test/resources/application.yml
@@ -0,0 +1,3 @@
+spring:
+ ai:
+ openai.api-key: ${OPEN_AI_KEY}
\ No newline at end of file