尝试在 tomcat 中显示某些内容时出现错误 404
Error 404, when trying to show something in tomcat
我做了这一切 video:
但是当我输入时:http://localhost:8080/spring-sample-1.0-SNAPSHOT/hello
出现这个错误:
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.
我唯一改变的是:tomcat/bin/setclasspath.bat,我在那里添加了一行:
set JRE_HOME=C:\Program Files\Java\jre1.8.0_271
因为没有它服务器不会启动
好的,所以我的应用程序真的很简单,我在 java 15 创建了 mvn 项目,然后是两个 类:
配置:
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];
}
}
您好:
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>
然后我将所有内容打包到 war 文件中并将其添加到 tomcat 管理器中:WAR 文件部署
之后我在tomcat模块
中点击了/spring-sample-1.0-SNAPSHOT
然后在最后输入 hello
知道出了什么问题吗? :/
首先,我建议为 OS 设置 JAVA_HOME 或 JRE_HOME 环境变量。
更多详情:https://confluence.atlassian.com/doc/setting-the-java_home-variable-in-windows-8895.html
在任何情况下更改 tomcat 的文件都不是好主意 :)
要解决无法访问 url 的问题,请检查:
已安装 tomcat:
- 作为规则,war 文件的名称是上下文名称和 url(spring-sample-1.0-SNAPSHOT) 的一部分,但该名称可以在 context.xml(文档:https://tomcat.apache.org/tomcat-8.0-doc/config/context.html)
- 打开管理器 window 正如您在视频中看到的那样 (http://localhost:8080/manager/html) 并找到您的 Web 应用程序 link
对于嵌入式 tomcat:
- 上下文路径应该类似于 pom.xml
中的 artifactId
@saver
部署时从 tomcat 登录:
21-Dec-2020 16:49:22.227 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war]
21-Dec-2020 16:49:24.113 INFO [http-nio-8000-exec-17] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
21-Dec-2020 16:49:24.138 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war] has finished in [1,907] ms
那么我应该使用哪个版本的 JRE 和 JDK?
@daniep kajoi 您应该在 java 15 上为 tomcat 设置路径,或者在 1.8 版本的 pom.xml 中更改 maven.compiler.source 属性 - 两个选项之一。
我在你的日志中看到你的路径是 'spring-sample3-1.0-SNAPSHOT.war'
21-Dec-2020 16:49:22.227 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war]
21-Dec-2020 16:49:24.113 INFO [http-nio-8000-exec-17] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
21-Dec-2020 16:49:24.138 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war] has finished in [1,907] ms
尝试打开url:http://localhost:8080/spring-sample3-1.0-SNAPSHOT.war/hello
我发现了问题:在配置 class 中,您为 servlet 映射和 servlet 配置 class 提供了错误的值。
请更改配置 class 如下:
@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[]{"/"};
}
}
我做了这一切 video:
但是当我输入时:http://localhost:8080/spring-sample-1.0-SNAPSHOT/hello
出现这个错误:
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.
我唯一改变的是:tomcat/bin/setclasspath.bat,我在那里添加了一行:
set JRE_HOME=C:\Program Files\Java\jre1.8.0_271
因为没有它服务器不会启动
好的,所以我的应用程序真的很简单,我在 java 15 创建了 mvn 项目,然后是两个 类:
配置:
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];
}
}
您好:
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>
然后我将所有内容打包到 war 文件中并将其添加到 tomcat 管理器中:WAR 文件部署
之后我在tomcat模块
中点击了/spring-sample-1.0-SNAPSHOT然后在最后输入 hello
知道出了什么问题吗? :/
首先,我建议为 OS 设置 JAVA_HOME 或 JRE_HOME 环境变量。 更多详情:https://confluence.atlassian.com/doc/setting-the-java_home-variable-in-windows-8895.html 在任何情况下更改 tomcat 的文件都不是好主意 :)
要解决无法访问 url 的问题,请检查:
已安装 tomcat:
- 作为规则,war 文件的名称是上下文名称和 url(spring-sample-1.0-SNAPSHOT) 的一部分,但该名称可以在 context.xml(文档:https://tomcat.apache.org/tomcat-8.0-doc/config/context.html)
- 打开管理器 window 正如您在视频中看到的那样 (http://localhost:8080/manager/html) 并找到您的 Web 应用程序 link
对于嵌入式 tomcat:
- 上下文路径应该类似于 pom.xml 中的 artifactId
@saver
部署时从 tomcat 登录:
21-Dec-2020 16:49:22.227 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war]
21-Dec-2020 16:49:24.113 INFO [http-nio-8000-exec-17] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
21-Dec-2020 16:49:24.138 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war] has finished in [1,907] ms
那么我应该使用哪个版本的 JRE 和 JDK?
@daniep kajoi 您应该在 java 15 上为 tomcat 设置路径,或者在 1.8 版本的 pom.xml 中更改 maven.compiler.source 属性 - 两个选项之一。 我在你的日志中看到你的路径是 'spring-sample3-1.0-SNAPSHOT.war'
21-Dec-2020 16:49:22.227 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war]
21-Dec-2020 16:49:24.113 INFO [http-nio-8000-exec-17] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
21-Dec-2020 16:49:24.138 INFO [http-nio-8000-exec-17] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\Users\Damian\Desktop\JAVA\apache-tomcat-8.5.61\webapps\spring-sample3-1.0-SNAPSHOT.war] has finished in [1,907] ms
尝试打开url:http://localhost:8080/spring-sample3-1.0-SNAPSHOT.war/hello
我发现了问题:在配置 class 中,您为 servlet 映射和 servlet 配置 class 提供了错误的值。 请更改配置 class 如下:
@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[]{"/"};
}
}