liveBook · Manning

REST

As explained in Spring AI in Action

A REST service is a network service that exposes functionality or resources through REST-style HTTP endpoints.

A REST service exposes application functionality through HTTP endpoints. In a Spring application, a REST API can accept a question, delegate it to an AI service, and return the generated answer as JSON. section 1.1 section 1.1.2

A minimal Spring REST endpoint connects these pieces:

  • A POST request targets /ask and carries a Question object in its request body. section 1.1.2
  • A controller delegates the request to BoardGameService rather than performing the AI interaction itself. section 1.1.2
  • The service submits the question through ChatClient and wraps the generated text in an Answer record. section 1.1.2

Question-answer endpoint

A client sends a question to POST /ask. AskController accepts the JSON request, calls BoardGameService.askQuestion(), and returns an Answer as application/json. The service uses ChatClient's fluent prompt(), user(), call(), and content() operations to obtain the response. section 1.1.2

Listing 1.3 A controller that handles requests working with BoardGameService
package com.example.boardgamebuddy;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AskController {

  private final BoardGameService boardGameService;

  public AskController(BoardGameService boardGameService) {  #1
    this.boardGameService = boardGameService;
  }

  @PostMapping(path="/ask", produces="application/json")
  public Answer ask(@RequestBody Question question) {
      return boardGameService.askQuestion(question);  #2
  }

}

Inspect the controller listing to see request-body binding, endpoint mapping, dependency injection, and service delegation together. section 1.1.2

How Spring AI in Action covers it

How deep it goes
Demonstrates endpoint implementation with Spring annotations, request records, dependency injection, service delegation, and JSON output.
Examples
Uses a POST /ask controller with Question and Answer Java records to demonstrate an AI-backed endpoint.
What it assumes
Assumes Java and Spring dependency injection while defining request and response records as simple single-string types.
What you can do after
Build an AI-backed REST endpoint that accepts JSON questions and returns JSON answers through Spring components.
How it connects
Connects REST APIs with HTTP clients, Spring AI, ChatClient, LLMs, JSON, and dependency injection.

Also explained in

Compare how 22 books explain REST →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring AI in Action. The section links point to the parts of the book it draws on.