Add /api/greetings endpoint with optional name

This commit is contained in:
Anupama Natarajan 2025-04-24 07:10:15 +12:00
parent 0c88f916db
commit 8af91504f0
3 changed files with 44 additions and 0 deletions

View file

@ -163,3 +163,13 @@ For additional details, please refer to the blog post [Hello DCO, Goodbye CLA: S
## License
The Spring PetClinic sample application is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0).
### User Story
As a pet clinic administrator, I want to add a `/greetings` API endpoint that returns a personalized welcome message so that I can display it on the homepage.
Acceptance Criteria:
- Endpoint: `/api/greetings`
- Method: GET
- Optional query param: `name`
- Returns: "Welcome to the PetClinic, <name>!" or "Welcome to the PetClinic!" if no name is passed.

View file

@ -0,0 +1,13 @@
@RestController
@RequestMapping("/api")
public class GreetingController {
@GetMapping("/greetings")
public String greet(@RequestParam(required = false) String name) {
if (name == null || name.isEmpty()) {
return "Hello, World!";
} else {
return "Hello, " + name + "!";
}
}
}

View file

@ -0,0 +1,21 @@
@SpringBootTest
@AutoConfigureMockMvc
public class GreetingControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testGreetWithName() throws Exception {
mockMvc.perform(get("/api/greetings?name=Anu"))
.andExpect(status().isOk())
.andExpect(content().string("Welcome to the PetClinic, Anu!"));
}
@Test
public void testGreetWithoutName() throws Exception {
mockMvc.perform(get("/api/greetings"))
.andExpect(status().isOk())
.andExpect(content().string("Welcome to the PetClinic!"));
}
}