CodexBloom - Programming Q&A Platform

Unexpected 404 scenarios for REST API Endpoint in Spring Boot with Swagger Integration

πŸ‘€ Views: 2 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-07
spring-boot swagger rest api Java

I'm collaborating on a project where I'm currently working on a Spring Boot application (version 2.5.4) where I'm integrating Swagger for API documentation. I created a REST API endpoint to retrieve user details, but I'm working with an unexpected 404 behavior when trying to access it through Swagger UI. I've confirmed that the endpoint is correctly defined, and I'm using the following code snippet to define it: ```java @RestController @RequestMapping("/api/v1/users") public class UserController { @GetMapping("/{id}") public ResponseEntity<User> getUserById(@PathVariable Long id) { User user = userService.findById(id); return ResponseEntity.ok(user); } } ``` In my application properties file, I have the following configuration for Swagger: ```properties springdoc.api-docs.path=/v3/api-docs springdoc.swagger-ui.path=/swagger-ui.html ``` When I launch the application and navigate to `/swagger-ui.html`, I can see the API documentation, but clicking on the `GET /api/v1/users/{id}` link leads to a 404 behavior. I tried accessing the endpoint directly via Postman with a valid user ID, and I get the same 404 response: ```plaintext HTTP/1.1 404 Not Found Content-Type: application/json;charset=UTF-8 Content-Length: 0 ``` I've checked the following: - The `UserController` is annotated with `@RestController`, and it’s in a package scanned by Spring Boot. - The application runs on port 8080 and I am making the request to `http://localhost:8080/api/v1/users/1`. - There are no typos in the URL. I also considered potential CORS issues, but I added the necessary configuration and that doesn't seem to be the question. Could this be related to the way Swagger generates the API paths or something specific to the Spring Boot version? Any insights on what might be causing the 404 behavior would be greatly appreciated! I'm working with Java in a Docker container on Windows 11.