I’m stuck with a Spring Boot app I’m building. The frontend (JSP) works fine when I run it on Tomcat. But when I try to access the backend API, like http://localhost:8090/orders
, I keep getting a 404 error.
I’ve set up my @RequestMapping
correctly (I think). Here’s a quick look at my setup:
@RestController
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("/products")
public List<Product> getAllProducts() {
return productService.getAllProducts();
}
}
My application.properties
has the database info:
spring.datasource.url=jdbc:mysql://localhost:3306/myshop
spring.datasource.username=admin
spring.datasource.password=secretpass
I’ve got the MySQL connector in my pom.xml
:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
The service and repository layers look okay too. No errors in the console when starting up.
Any ideas why I’m getting this 404? I’m totally stumped and could really use some help figuring this out. Thanks!
hmmm, interesting issue! have u checked ur application.properties for the server.port? sometimes it defaults to 8080 instead of 8090. also, are u sure ur hitting the right endpoint? ur code shows /products but ur trying /orders. maybe thats the problem? lemme know if that helps!
Have you verified that your Spring Boot application is actually running on port 8090? It’s possible your app might be starting on a different port. Check your console output when launching the application for the actual port number.
Also, ensure your @ComponentScan
annotation in your main application class is set up correctly to scan all the necessary packages containing your controllers. Sometimes, controllers can be missed if they’re in unexpected locations.
Lastly, double-check your project structure. Make sure your controller classes are in the correct package hierarchy, typically under your main application package. Spring Boot auto-configuration relies on proper package structure to detect and register your components.
hey, have u tried using @RequestMapping at the class level? like @RequestMapping(“/api”) above ur class declaration? that way u can group related endpoints. also, double-check ur component scanning - sometimes spring misses stuff. good luck!