Issue
I did everything in this video:
But when I'm typing:http://localhost:8080/spring-sample-1.0-SNAPSHOT/hello
This error shows up:
HTTP Status 404 – Not Found
Type Status Report
Message The requested resource [/spring-sample-1.0-SNAPSHOT/hello] 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.
The only thing I changed was in: tomcat/bin/setclasspath.bat, I added one line there:
set JRE_HOME=C:\Program Files\Java\jre1.8.0_271
Because without that the server does not start
Okey, so my application is really simple, i created mvn project in java 15, then two classes:
Config:
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.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Configuration
@ComponentScan({"app"})
@EnableWebMvc
public class Config extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[0];
}
@Override
protected String[] getServletMappings() {
return new String[0];
}
}
Hello:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@GetMapping("/hello")
public String get(){
return "Bycza zagroda!";
}
}
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.bykowski</groupId>
<artifactId>spring-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Then I packed everything in war file and add it in tomcat manager here: WAR file to deploy
After that I clicked /spring-sample-1.0-SNAPSHOT in tomcat module
and then typed hello at the end
Any ideas what is going wrong? :/
Solution
I found the issue: in Config class you provided incorrect values for servlet mapping and servlet config class. Please change Config class as follows:
@Configuration
@ComponentScan({"app"})
@EnableWebMvc
public class Config extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {Config.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
Answered By - saver
Answer Checked By - Candace Johnson (JavaFixing Volunteer)