Skip to content

refactor(AI): migrate chat controllers to RestClient - #360

Merged
hirokiterashima merged 1 commit into
developfrom
refactor/restclient-llm-controllers
Sep 30, 2026
Merged

hirokiterashima merged 1 commit into
developfrom
refactor/restclient-llm-controllers

Conversation

@hirokiterashima

Copy link
Copy Markdown
Member

Summary

This PR modernizes outbound HTTP communication across the AI chat controllers by replacing legacy java.net.HttpURLConnection implementations with Spring 6 / Boot 3 RestClient. It also adopts constructor injection, programs against domain interfaces in ChatbotController, and introduces comprehensive unit tests using MockRestServiceServer.

Changes

1. Modernize Outgoing HTTP Clients with RestClient

  • ChatGptController.java:

    • Replaced HttpURLConnection, deprecated new URL(...), and manual byte/character stream reading with Spring's declarative, fluent RestClient.
    • Switched from field injection (@Value) to constructor injection (RestClient.Builder, @Value("${openai.api.key:}"), and @Value("${openai.chat.api.url:}")).
    • Uses declarative headers (HttpHeaders.AUTHORIZATION), MediaType.APPLICATION_JSON, and .retrieve().body(String.class).
  • AWSBedrockController.java:

    • Replaced field-injected @Autowired Environment appProperties with constructor injection alongside RestClient.Builder.
    • Replaced HttpURLConnection boilerplate with RestClient.post().
    • Added produces = "application/json;charset=UTF-8" to @PostMapping for consistency.

2. Interface Decoupling in ChatbotController.java

  • Updated method signatures for /chats/{run}/{workgroup}/** endpoints to bind to domain interfaces (Run, Workgroup) rather than concrete implementation classes (RunImpl, WorkgroupImpl).

3. Unit Test Coverage

  • ChatGptControllerTest.java:
    • Tests successful chat completions request/response exchange using MockRestServiceServer.
    • Validates URL endpoint, Authorization: Bearer <key> header, JSON content-type, and payload matching.
    • Validates exception handling when openai.api.key is missing or null.
  • AWSBedrockControllerTest.java:
    • Tests successful request dispatch to {aws.bedrock.runtime.endpoint}/openai/v1/chat/completions.
    • Validates authorization header and body forwarding.
    • Validates error handling for missing aws.bedrock.api.key and missing aws.bedrock.runtime.endpoint.

Affected Files

  • src/main/java/org/wise/portal/presentation/web/AWSBedrockController.java
  • src/main/java/org/wise/portal/presentation/web/controllers/ChatGptController.java
  • src/main/java/org/wise/portal/presentation/web/controllers/ChatbotController.java
  • src/test/java/org/wise/portal/presentation/web/AWSBedrockControllerTest.java
  • src/test/java/org/wise/portal/presentation/web/controllers/ChatGptControllerTest.java

Verification

  • mvn compile and mvn test-compile built with zero errors.
  • Executed all unit tests: ChatGptControllerTest, AWSBedrockControllerTest, ChatTest, ChatMessageTest, and CRaterServiceTest passed successfully.

@hirokiterashima hirokiterashima self-assigned this Sep 30, 2026
@hirokiterashima
hirokiterashima merged commit d9943dc into develop Sep 30, 2026
2 checks passed
@hirokiterashima
hirokiterashima deleted the refactor/restclient-llm-controllers branch September 30, 2026 23:25
@hirokiterashima

Copy link
Copy Markdown
Member Author

🎉 This PR is included in version 1.25.6 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant