Encountering a 404 Page in Spring Boot Application

I am developing an application using Spring Boot, which includes a JSP front end and a Java back end. Once I deploy the application on a Tomcat server, the front end loads successfully. However, attempting to access a specific URL to evaluate the back end functionality results in a 404 error.

Example URL: http://localhost:8090/orders

I’ve ensured that the @RequestMapping paths are set up correctly and I am employing Workbench for my database management.

Here is part of my application.properties file:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/csse_ass
spring.datasource.username=root
spring.datasource.password=1234

spring.jpa.hibernate.ddl-auto=create
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true

In my pom.xml file, I’ve included the necessary dependency:

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.11</version>
</dependency>

Here’s the controller snippet:

private OrderService orderService;

@RequestMapping("/orders")
public List<Order> fetchAllOrders() {
    return orderService.fetchAllOrders();
}

In the service layer:

@Autowired
private OrderRepository orderRepo;

public List<Order> fetchAllOrders() {
    List<Order> orderList = new ArrayList<>();
    for (Order order : orderRepo.findAll()) {
        orderList.add(order);
    }
    return orderList;
}

And the repository interface looks like this:

public interface OrderRepo extends CrudRepository<Order, String> {}

I have an Order.java class as well, but I don’t believe it plays a role in this issue. I’m not encountering any errors in my terminal apart from the usual Tomcat notices.

Question: What could be causing the redirection to the 404 page as mentioned at the start? I’m seeking assistance to resolve this issue as I am unable to identify the root cause.

Brilliant app you got there! If your OrderService isn’t @Autowired, your app might not call it like you intend. Also, check the logs if the application actually hit any “orders” URL endpoint. Adding some logger debug lines might reveal something being missed!

Hey, 404 usually means the requested URL cannot be matched with any route in your controller. Double check URL mapping in your controller. Also make sure there’s a relevant @Controller or @RestController annotation above your class declaration. Without it, Spring doesn’t map URLs to your methods!

When encountering a 404 error, another aspect to verify is the package structure and component scanning within your Spring Boot application. Ensure that the @SpringBootApplication annotation or your configuration class is scanning the package where your controller resides. This can be corrected by using the scanBasePackages property if necessary. Additionally, confirm that your controller is not missing any essential imports, and it might be worth checking any security configurations that could inadvertently be restricting access to your endpoints.

Another potential issue might be related to the pom.xml file you provided. Ensure that the spring-boot-starter-web dependency is included, as this is crucial for using Spring MVC and serving requests. Without it, even though Tomcat is a Servlet container, your controller endpoints might not be accessible. Additionally, double-check the Tomcat deployment descriptor (web.xml) if you have one, since any misconfiguration here might prevent certain URLs from being reachable. Confirm that the URL paths within Tomcat settings align with those in your application.