1

I'm following an Udemy Course of Spring and Spring Boot and we're building an Application Web App. We build these classes:

  • WebConfig.java
package it.fogliafabrizio.corso.web;

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

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "it.fogliafabrizio.corso.web.controller") // Rilevamento automatico dei controller e li gestisce come bean
public class WebConfig {

}

  • AppInitializer.java
package it.fogliafabrizio.corso.web;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FrameworkServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        /* Qual è la classe che deve utilizzare per accedere a tutte le configurazioni dell'app
         * web
         */
        return new Class<?>[] {WebConfig.class}; //Torna un array di Class Generico della Classe Config 
    }

    @Override
    protected String[] getServletMappings() {
        /* Qual'è il path che deve intercettare la DispatcherServlet 
         * nella nostra applicazione
         */
        
        return new String[]{"/"}; // tutto quello che arriva all'appl. deve essere gestito
    }

    @Override
    /* Right-click > Source > Override/Implements Method
     * AbstractDispatcherInitializer -> trovi metodo che prende in ingresso WebApplicationContext
     */
    protected FrameworkServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
        /* Creiamo un oggetto di tipo DispatcherServlet che prende in ingresso
         * AppContext
         * Impostiamo il metodo setTrow... true in modo che la WebApp generà eccezione
         * al posto dell'errore 404
         * e torniamo l'oggeto metodo
         */
        DispatcherServlet ds = new DispatcherServlet(servletAppContext);
        ds.setThrowExceptionIfNoHandlerFound(true);
        return ds;
    }
    
    

}

  • Exemple of Controller
package it.fogliafabrizio.corso.web.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rest")
public class WelcomeRestController {
    
    @RequestMapping("/welcome")
    public String welcome() {
        return "{message: Welcome}"; //ESEMPIO JSON
    }

}

Using Tomcat 9.0 and Spring Boot Suite, I can see that server downloads and deploy correctly the web application (INFORMAZIONI: Deploying web application directory [C:\Users\Fabrizio\Documents\Spring\Udemy - Guida Completa\apache-tomcat-9.0.71\webapps\ROOT]) and, if I try to search http://localhost:8080, I can see the Home Page of TomCat.

If I search http://localhost:8080/rest/welcome, I have an 404 Error.

In the course I can see that, in him console, he have Initializing Spring DispatcherServlet 'dispatcher that I don't have in mine.. I can imagine that this is the problem of why Controller can see the request and do them method..

Can anybody try to help me please?

Thank you.

I try to delete and restart the server but nothing change.. I have the same problems..

I list here my 'pom.xml':

<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>it.fogliafabrizio.corso.web</groupId>
  <artifactId>corsospringweb</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>corsospringweb</name>
  
  
  <dependencies>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.2</version>
     </dependency>
     
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.0.4</version>
    </dependency>
    
    <dependency>
        <!-- Core -->
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>6.0.4</version>
    </dependency>
    
    <dependency>
        <!-- Beans -->
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>6.0.4</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>6.0.4</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>6.0.4</version>
    </dependency>
    
    <dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
        <version>6.0.0</version>
        <scope>provided</scope>
    </dependency>


    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
    </dependency>
    
  </dependencies>
  <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.2</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
         </plugin> 
         
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
         </plugin>
      </plugins>
  </build>
</project>
  • 1
    Spring Framework 6.0 goes GA (https://spring.io/blog/2022/11/16/spring-framework-6-0-goes-ga) , **Spring Framework 6.0 comes with a Java 17+ baseline** , JDK 17 not JDK 8. **This provides access to the latest web containers such as Tomcat 10.1**, Tomcat 10.x not Tomcat 9.x – life888888 Feb 15 '23 at 10:55
  • If you are following a course **follow the coursre, including its dependencies**. You are following a course and decided to change some dependencies leading to this. In short you also need Tomcat 10. Also if this is a course that tells you to add plugins as dependencies, I would go look for a different course... – M. Deinum Feb 15 '23 at 11:08
  • @M.Deinum you are right.. i try to change my dependencies with the same of the course (that are older) and always work.. maybe I try to change course.. I find that course because I was searching somebody that help me to study Spring Theory before to write code.. However Thank You :) – Fabrizio Foglia Feb 15 '23 at 11:26

0 Answers0