gpt4 book ai didi

java - 嵌入式Tomcat + Spring文件上传: HTTP 404

转载 作者:行者123 更新时间:2023-11-28 22:52:35 27 4
gpt4 key购买 nike

如果我通过 Eclipse 启动 myApp throw Internet Browser,我可以打开它,但是,虽然我可以启动嵌入式服务器 throw 命令行 (java -jar myApp.jar),但我得到 HTTP 状态 404。可能是上下文有问题但是我不知道哪里出了问题。

请参阅下面 Eclipse 和 myApp.jar 中的结构。该应用程序基本上由 5 个文件组成:两个文件位于 com.mycompany.myapp.batchs.AuthFileUpload(带有 main 方法的 App.class 和作为非常简单的 Controller 的 FileUploadController.class)。其他三个文件App-servlet.xml、web.xml和index.jsp放在WEB-INF中。

其他可能的原因可能是我应该直接指向“tomcat.addWebapp("/", "the_place_where_classes_can_be_found_in_myApp.jar)"中的类。如果是这样,我该怎么做?

Index.jsp 和 FileUploadController.java 太简单了,在这里显示没有意义。

应用类

 public static void main(String[] args) throws IOException,
ServletException, LifecycleException {
Tomcat tomcat = new Tomcat();

tomcat.setPort(8080);


tomcat.setBaseDir("C:\\temp\\");
tomcat.addWebapp("/", "C:\\temp\\");
tomcat.start();
tomcat.getServer().await();

}

App-servlet.xml

<context:component-scan base-package="com.mycompany.myapp.batchs.AuthFileUpload" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

网络.xml

   <display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>App</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>App</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

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>com.myCompany.myApp.batchs</groupId>
<artifactId>AuthFileUpload</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AuthFileUpload</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<tomcat.version>8.0.32</tomcat.version>
<java.version>1.8</java.version>
<maven.compiler.plugin.version>2.1</maven.compiler.plugin.version>
<spring.version>4.2.5.RELEASE</spring.version>
</properties>


<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>


</dependencies>
<build>
<finalName>embeddedApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
<mainClass>com.myCompany.myApp.batchs.AuthFileUpload.App4</mainClass>

<name>App4</name>

</program>
</programs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

enter image description here

enter image description here

enter image description here

*** 添加于 2016/March/04

Executable Jar

*** 添加于 2016/March/07 下午 5 点巴西利亚时区

C:\STS\wsRestTemplate\TestDeployment\target>cd ..

C:\STS\wsRestTemplate\TestDeployment>java -version
Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)

C:\STS\wsRestTemplate\TestDeployment>mvn clean package
Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building AuthFileUpload 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ AuthFileUpload ---
[INFO] Deleting C:\STS\wsRestTemplate\TestDeployment\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ AuthFileUp
load ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ AuthFileUploa
d ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\STS\wsRestTemplate\TestDeployment\target\c
lasses
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Au
thFileUpload ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\STS\wsRestTemplate\TestDeployment\
src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ AuthF
ileUpload ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ AuthFileUpload ---

[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ AuthFileUpload ---
[INFO] Building jar: C:\STS\wsRestTemplate\TestDeployment\target\embeddedApp.jar

[INFO]
[INFO] --- appassembler-maven-plugin:1.10:assemble (default) @ AuthFileUpload --
-
[WARNING] The usage of program name (App) is deprecated. Please use program.id i
nstead.
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\springframework\s
pring-web\4.2.5.RELEASE\spring-web-4.2.5.RELEASE.jar to C:\STS\wsRestTemplate\Te
stDeployment\target\repo\org\springframework\spring-web\4.2.5.RELEASE\spring-web
-4.2.5.RELEASE.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\springframework\s
pring-aop\4.2.5.RELEASE\spring-aop-4.2.5.RELEASE.jar to C:\STS\wsRestTemplate\Te
stDeployment\target\repo\org\springframework\spring-aop\4.2.5.RELEASE\spring-aop
-4.2.5.RELEASE.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\aopalliance\aopallian
ce\1.0\aopalliance-1.0.jar to C:\STS\wsRestTemplate\TestDeployment\target\repo\a
opalliance\aopalliance\1.0\aopalliance-1.0.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\springframework\s
pring-beans\4.2.5.RELEASE\spring-beans-4.2.5.RELEASE.jar to C:\STS\wsRestTemplat
e\TestDeployment\target\repo\org\springframework\spring-beans\4.2.5.RELEASE\spri
ng-beans-4.2.5.RELEASE.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\springframework\s
pring-context\4.2.5.RELEASE\spring-context-4.2.5.RELEASE.jar to C:\STS\wsRestTem
plate\TestDeployment\target\repo\org\springframework\spring-context\4.2.5.RELEAS
E\spring-context-4.2.5.RELEASE.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\springframework\s
pring-core\4.2.5.RELEASE\spring-core-4.2.5.RELEASE.jar to C:\STS\wsRestTemplate\
TestDeployment\target\repo\org\springframework\spring-core\4.2.5.RELEASE\spring-
core-4.2.5.RELEASE.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\commons-logging\commo
ns-logging\1.2\commons-logging-1.2.jar to C:\STS\wsRestTemplate\TestDeployment\t
arget\repo\commons-logging\commons-logging\1.2\commons-logging-1.2.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\hibernate\hiberna
te-validator-annotation-processor\5.2.4.Final\hibernate-validator-annotation-pro
cessor-5.2.4.Final.jar to C:\STS\wsRestTemplate\TestDeployment\target\repo\org\h
ibernate\hibernate-validator-annotation-processor\5.2.4.Final\hibernate-validato
r-annotation-processor-5.2.4.Final.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\springframework\s
pring-webmvc\4.2.5.RELEASE\spring-webmvc-4.2.5.RELEASE.jar to C:\STS\wsRestTempl
ate\TestDeployment\target\repo\org\springframework\spring-webmvc\4.2.5.RELEASE\s
pring-webmvc-4.2.5.RELEASE.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\springframework\s
pring-expression\4.2.5.RELEASE\spring-expression-4.2.5.RELEASE.jar to C:\STS\wsR
estTemplate\TestDeployment\target\repo\org\springframework\spring-expression\4.2
.5.RELEASE\spring-expression-4.2.5.RELEASE.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\emb
ed\tomcat-embed-core\8.0.32\tomcat-embed-core-8.0.32.jar to C:\STS\wsRestTemplat
e\TestDeployment\target\repo\org\apache\tomcat\embed\tomcat-embed-core\8.0.32\to
mcat-embed-core-8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\emb
ed\tomcat-embed-logging-juli\8.0.32\tomcat-embed-logging-juli-8.0.32.jar to C:\S
TS\wsRestTemplate\TestDeployment\target\repo\org\apache\tomcat\embed\tomcat-embe
d-logging-juli\8.0.32\tomcat-embed-logging-juli-8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\emb
ed\tomcat-embed-jasper\8.0.32\tomcat-embed-jasper-8.0.32.jar to C:\STS\wsRestTem
plate\TestDeployment\target\repo\org\apache\tomcat\embed\tomcat-embed-jasper\8.0
.32\tomcat-embed-jasper-8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\emb
ed\tomcat-embed-el\8.0.32\tomcat-embed-el-8.0.32.jar to C:\STS\wsRestTemplate\Te
stDeployment\target\repo\org\apache\tomcat\embed\tomcat-embed-el\8.0.32\tomcat-e
mbed-el-8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\eclipse\jdt\core\
compiler\ecj\4.4.2\ecj-4.4.2.jar to C:\STS\wsRestTemplate\TestDeployment\target\
repo\org\eclipse\jdt\core\compiler\ecj\4.4.2\ecj-4.4.2.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\tom
cat-jasper\8.0.32\tomcat-jasper-8.0.32.jar to C:\STS\wsRestTemplate\TestDeployme
nt\target\repo\org\apache\tomcat\tomcat-jasper\8.0.32\tomcat-jasper-8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\tom
cat-servlet-api\8.0.32\tomcat-servlet-api-8.0.32.jar to C:\STS\wsRestTemplate\Te
stDeployment\target\repo\org\apache\tomcat\tomcat-servlet-api\8.0.32\tomcat-serv
let-api-8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\tom
cat-juli\8.0.32\tomcat-juli-8.0.32.jar to C:\STS\wsRestTemplate\TestDeployment\t
arget\repo\org\apache\tomcat\tomcat-juli\8.0.32\tomcat-juli-8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\tom
cat-el-api\8.0.32\tomcat-el-api-8.0.32.jar to C:\STS\wsRestTemplate\TestDeployme
nt\target\repo\org\apache\tomcat\tomcat-el-api\8.0.32\tomcat-el-api-8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\tom
cat-api\8.0.32\tomcat-api-8.0.32.jar to C:\STS\wsRestTemplate\TestDeployment\tar
get\repo\org\apache\tomcat\tomcat-api\8.0.32\tomcat-api-8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\tom
cat-util-scan\8.0.32\tomcat-util-scan-8.0.32.jar to C:\STS\wsRestTemplate\TestDe
ployment\target\repo\org\apache\tomcat\tomcat-util-scan\8.0.32\tomcat-util-scan-
8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\tom
cat-util\8.0.32\tomcat-util-8.0.32.jar to C:\STS\wsRestTemplate\TestDeployment\t
arget\repo\org\apache\tomcat\tomcat-util\8.0.32\tomcat-util-8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\tom
cat-jasper-el\8.0.32\tomcat-jasper-el-8.0.32.jar to C:\STS\wsRestTemplate\TestDe
ployment\target\repo\org\apache\tomcat\tomcat-jasper-el\8.0.32\tomcat-jasper-el-
8.0.32.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\apache\tomcat\tom
cat-jsp-api\8.0.32\tomcat-jsp-api-8.0.32.jar to C:\STS\wsRestTemplate\TestDeploy
ment\target\repo\org\apache\tomcat\tomcat-jsp-api\8.0.32\tomcat-jsp-api-8.0.32.j
ar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\commons-fileupload\co
mmons-fileupload\1.3.1\commons-fileupload-1.3.1.jar to C:\STS\wsRestTemplate\Tes
tDeployment\target\repo\commons-fileupload\commons-fileupload\1.3.1\commons-file
upload-1.3.1.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\commons-io\commons-io
\2.2\commons-io-2.2.jar to C:\STS\wsRestTemplate\TestDeployment\target\repo\comm
ons-io\commons-io\2.2\commons-io-2.2.jar
[INFO] Installing artifact C:\Users\e049447\.m2\repository\javax\servlet\javax.s
ervlet-api\3.0.1\javax.servlet-api-3.0.1.jar to C:\STS\wsRestTemplate\TestDeploy
ment\target\repo\javax\servlet\javax.servlet-api\3.0.1\javax.servlet-api-3.0.1.j
ar
[INFO] Installing artifact C:\STS\wsRestTemplate\TestDeployment\target\embeddedA
pp.jar to C:\STS\wsRestTemplate\TestDeployment\target\repo\com\mycompany\myapp\b
atchs\AuthFileUpload\0.0.1-SNAPSHOT\AuthFileUpload-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.899 s
[INFO] Finished at: 2016-03-07T13:50:14-06:00
[INFO] Final Memory: 22M/123M
[INFO] ------------------------------------------------------------------------

C:\STS\wsRestTemplate\TestDeployment>cd target

C:\STS\wsRestTemplate\TestDeployment\target>java -jar embeddedApp.jar
Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/catalina/L
ifecycleException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544
)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)

Caused by: java.lang.ClassNotFoundException: org.apache.catalina.LifecycleExcept
ion
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more

C:\STS\wsRestTemplate\TestDeployment\target>

在pom中添加了额外的插件

<finalName>embeddedApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>


<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.10</version>
<configuration>
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
<mainClass>com.mycompany.myapp.batchs.TestDeployment.App</mainClass>
<name>App</name>
</program>
</programs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mycompany.myapp.batchs.TestDeployment.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

</plugins>

最佳答案

您的应用结构有误。 Maven jar 项目没有 src/main/webapp 文件夹。

另一方面,为了让容器(不是类加载器)在 jar 中找到应用程序的 Web 资源 (index.jsp),您必须在 web.xml 中声明它是一个 servlet 3.0 webapp,并将资源放在 src/main/resources/META-INF/resources 中。参见 here了解更多详情。

试试这个:

  1. 确保 webapp 是 servlet 3.0 并且 index.jsp 是欢迎文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    ...
    </web-app>
  2. 将结构重构为:

    |-- src/main/resources
    | |-- WEB-INF/App-servlet.xml
    | |-- META-INF/resources
    | | | -- WEB-INF/web.xml
    | | | -- index.jsp

关于java - 嵌入式Tomcat + Spring文件上传: HTTP 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35784652/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com