Spring / Spring AI interview questions
How does PromptTemplate work in Spring AI?
PromptTemplate in Spring AI lets you define a prompt with named placeholders using {variableName} syntax and fill them in at runtime. This keeps prompt strings readable, testable as separate files, and decoupled from Java string concatenation.
// Inline template PromptTemplate template = new PromptTemplate( "Explain {concept} to a {level} developer in plain English." ); Prompt prompt = template.create(Map.of( "concept", "Java generics", "level", "junior" )); String answer = chatModel.call(prompt) .getResult().getOutput().getContent();
For multi-line prompts you should externalise the template to a classpath resource:
// src/main/resources/prompts/code-review.st PromptTemplate template = new PromptTemplate( new ClassPathResource("prompts/code-review.st") ); Prompt prompt = template.create(Map.of("code", sourceCode));
When using ChatClient, the fluent API supports inline variable substitution without constructing a PromptTemplate object explicitly:
chatClient.prompt() .user(u -> u.text("Summarise {topic} in three bullet points") .param("topic", userInput)) .call().content();
The {} placeholder syntax means you must escape any literal curly braces in your prompts as \{ and \}. Store template files in src/main/resources/prompts/ so prompt engineers can iterate on them without touching compiled Java.
More Related questions...