Scott Bogen
Scott Bogen

Reputation: 1

Spring Boot results in HTTP Status 404 – Not Found

My Spring Boot application runs, but when I access it in Postman, it only returns "HTTP Status 404 – Not Found" for every function call. I've tried many fixes, to no avail.

Folder structure

(image)

FlightPlannerApplication.java

package com.bogen.FlightPlanner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FlightPlannerApplication {
    public static void main(String[] args) {
        SpringApplication.run(FlightPlannerApplication.class, args);
    }
}

AirportAPI.java

package com.bogen.FlightPlanner.api;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import com.bogen.FlightPlanner.model.Airport;
import com.bogen.FlightPlanner.service.AirportService;

@RestController
@RequestMapping("AirportAPI")
public class AirportAPI {
    
    @Autowired
    private AirportService airportService;
    
    @GetMapping("/getAirports")
    public ResponseEntity<?> getAirports() {
        try {
            List<Airport> airports = airportService.getAirports();
            return new ResponseEntity<List<Airport>>(airports, HttpStatus.OK);
        }
        catch (Exception e) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
        }
    }
}

AirportServiceImpl.java

import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bogen.FlightPlanner.dao.AirportDAO;
import com.bogen.FlightPlanner.model.Airport;

@Service(value="airportService")
@Transactional
public class AirportServiceImpl implements AirportService {

    @Autowired
    AirportDAO airportDAO;
    
    @Override
    public List<Airport> getAirports() {
        List<Airport> airports = null;
        
        try {
            airports = airportDAO.getAirports();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return airports;
    }
}

And the AirportDAOImpl.java file is as you would expect, annotated with @Repository and containing an EntityManager annotated with @PersistenceContext.

Here is the console log:

2020-12-02 12:48:43.236  INFO 4452 --- [           main] c.b.F.FlightPlannerApplication           : Starting FlightPlannerApplication using Java 15.0.1 on DESKTOP-38S2QIU with PID 4452 (C:\Users\bogen\Downloads\FlightPlanner\target\classes started by bogen in C:\Users\bogen\Downloads\FlightPlanner)
2020-12-02 12:48:43.238  INFO 4452 --- [           main] c.b.F.FlightPlannerApplication           : No active profile set, falling back to default profiles: default
2020-12-02 12:48:43.552  INFO 4452 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-12-02 12:48:43.562  INFO 4452 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 5 ms. Found 0 JPA repository interfaces.
2020-12-02 12:48:43.894  INFO 4452 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-12-02 12:48:43.901  INFO 4452 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-02 12:48:43.901  INFO 4452 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.39]
2020-12-02 12:48:43.973  INFO 4452 --- [           main] o.a.c.c.C.[.[localhost].[/Project]       : Initializing Spring embedded WebApplicationContext
2020-12-02 12:48:43.973  INFO 4452 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 708 ms
2020-12-02 12:48:44.095  INFO 4452 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-12-02 12:48:44.123  INFO 4452 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-12-02 12:48:44.162  INFO 4452 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.23.Final
2020-12-02 12:48:44.263  INFO 4452 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/Project'
2020-12-02 12:48:44.265  INFO 4452 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-12-02 12:48:44.265  INFO 4452 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-12-02 12:48:44.268  INFO 4452 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2020-12-02 12:48:44.272  INFO 4452 --- [           main] c.b.F.FlightPlannerApplication           : Started FlightPlannerApplication in 1.272 seconds (JVM running for 1.516)
2020-12-02 12:48:44.348  INFO 4452 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-12-02 12:48:44.591  INFO 4452 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-12-02 12:48:44.610  INFO 4452 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2020-12-02 12:48:45.032  INFO 4452 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-12-02 12:48:45.038  INFO 4452 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'

And finally, the pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bogen</groupId>
    <artifactId>FlightPlanner</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>FlightPlanner</name>
    <description>Flight Planner project for Spring Boot</description>

    <properties>
        <java.version>15</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.1</version>
        </dependency>
                
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>2.4.0</version>
        </dependency>

        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

The application runs indefinitely, as expected. The GET request from Postman goes to the URL: http://localhost:8080/Project/AirportAPI/getAirports.

Any ideas?

Upvotes: 0

Views: 3605

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209102

Add the following dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-web is not enough. You need the Spring Boot wrapper with the autoconfiguration so that Spring MVC will be bootstrapped for you.

You can also remove the spring-web dependency, as it will be pulled for you by the spring-boot-start-web.

Upvotes: 3

Related Questions