springspring-mvcconfiguration

Java Based Annotation Configuration is not working


I am trying to learn Spring specifically for java based annotation configuration as I find it more easier to comprehend than xml based configuration however no matter how many videos I watch or how many guides I follow it does seems to work. All I want is the flow to reach my add method during debugging. The major issue is that there is no exact error other than just :

HTTP Status 404 – Not Found
Type Status Report

Message The requested resource [/SpringTesting/add] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/9.0.95

I am using Apache tomcat 9.0.95 as my server.

Please help me with this issues. Below are my classes:

package com.test.Configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@ComponentScan("com.test")
@EnableWebMvc
public class AppConfig extends WebMvcConfigurationSupport {
    
     public AppConfig() {
            System.out.println("AppConfig has been initialized!");
        }

}
package com.test.Configuration;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[] { "/" };
    }

}

package com.test.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class FirstController {

    @RequestMapping("/add")
    public void addNumbers(@RequestParam("val1") int val1, @RequestParam("val2") int val2) {
        System.out.println("Reached");//Debug Breakpoint here
    }

}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Web MVC Testing</title>
</head>
<body>
    <form action="add">
        <input type="number" name="val1"><br> 
        <input type="number"name="val2"><br> 
        <input type="submit"><br>
    </form>
</body>
</html>
<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>
    <groupId>SpringTesting</groupId>
    <artifactId>SpringTesting</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.13.0</version>
                <configuration>
                    <release>21</release>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.3</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- Spring Dependency -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.2.0</version>
        </dependency>
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.36</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

My project structure is:

src
 └── main
      └── java
           └── com
                └── test
                     ├── Configuration
                     │     ├── AppConfig.java
                     │     ├── AppInitializer.java
                     ├── Controller
                     │     └── FirstController.java
          webapp
           └──index.jsp

Solution

  • Spring 6.x requires a JakartaEE compatible server. You are using Tomcat 9 which is only JavaEE compatible. You need to use Tomcat 10 or higher.