Search code examples
javaspringspring-bootapihttp-status-code-404

Spring boot application returns 404 without entering controller class


Spring boot application returns 404 Not found error when calling API endpoints without even entering the controller class

I have gone through the possible solutions which can be found on the StackOverflow

possible solutions I have tested

  1. added and removed @RequestMapping at the class level
  2. tested with both
GetMapping("/get")

and

RequestMapping("/get")

and even with

GetMapping(value = "/get")
  1. added @ComponentScan even when my folder structure is as per the guidelines.

  2. tried with ResponseEntity<Object> and @RequestBody Annotation

    I've tried almost all of the solutions I got from various platforms. Spring boot application API endpoints should call the corresponding methods based on their paths configured. But in my application, it throws an error with '404 Not Found' when any endpoint called. It seems the endpoints are not registered in the spring application

controller Class

@RestController
    @RequestMapping(BACK_OFFICE_BASE_PATH)
    public class ManagementController {
    
        @Autowired
        ManagementService managementService;
    
        @GetMapping(  SLASH_PATH)
        public List<RateTemp> getUpdateRequests(){
            return managementService.getUpdateRequests();
        }
    
        @PostMapping(  SLASH_PATH)
        public RateTemp addRate(@RequestBody RateTemp body) throws BadRequestException {
            if(body != null){
                return managementService.addRate(body);
            }else throw new BadRequestException(ILLEGAL_REQUEST_FIELDS,"");
        }
    }

pom.xml

<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>
<properties>
        <java.version>1.8</java.version>
        <springfox-version>3.0.0</springfox-version>
        <log4j2.version>2.16.0</log4j2.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-explorer</artifactId>
        </dependency>
        <dependency>
            <groupId>com.warrenstrange</groupId>
            <artifactId>googleauth</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--springfox for swagger   -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-oas</artifactId>
            <version>${springfox-version}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox-version}</version>
        </dependency>

        <dependency>
            <groupId>com.github.joschi.jackson</groupId>
            <artifactId>jackson-datatype-threetenbp</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

Solution

  • I've found a solution for my problem without even understanding it. Anyway, it started working when I assigned a separate path for each and every method. In my previous code, I have assigned the same path value for different HTTP methods(GET, POST, PUT, etc.). I have done this before, but I never got this error ever. Im trying to understand the cause of this error.

    thanks for your suggestion

    @RestController
        @RequestMapping(BACK_OFFICE_BASE_PATH)
        public class ManagementController {
        
            @Autowired
            ManagementService managementService;
        
            @GetMapping(  SLASH_PATH + "add")
            public List<RateTemp> getUpdateRequests(){
                return managementService.getUpdateRequests();
            }
        
            @PostMapping(  SLASH_PATH + "update")
            public RateTemp addRate(@RequestBody RateTemp body) throws BadRequestException {
                if(body != null){
                    return managementService.addRate(body);
                }else throw new BadRequestException(ILLEGAL_REQUEST_FIELDS,"");
            }
        }