Embedded Tomcat + Spring file-upload: HTTP 404

旧巷老猫 提交于 2019-12-11 23:26:43

问题


I can open myApp throw Internet Browser if I start it via Eclipse, but, although I can start the Embedded server throw command line (java -jar myApp.jar) I am getting HTTP Status 404. Probably some issue with context but I can't figure out what is wrong.

Kindly, see below the structure in Eclipse and in myApp.jar. The application is basically compound by 5 files: two placed in com.mycompany.myapp.batchs.AuthFileUpload (App.class with main method and FileUploadController.class as a very simple controller). Other three files, App-servlet.xml, web.xml and index.jsp placed in WEB-INF.

Other possible reason can be I should point straigh to the classes in "tomcat.addWebapp("/", "the_place_where_classes_can_be_found_in_myApp.jar)". If so, how can I do it?

Index.jsp and the FileUploadController.java are too simple so it is pointless show both here.

App class

 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" />

web.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>

***Added in 2016/March/04

*** Added in 2016/March/07 at 5pm Brasilia timezone

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>

Extra plugin added in 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>

回答1:


Your app structure is wrong. Maven jar projects do not have a src/main/webapp folder.

On the other hand, in order to the container (not the classloader) finding the web resources (index.jsp) of your application in a jar, you must declare in web.xml that it is a servlet 3.0 webapp and place the resources in src/main/resources/META-INF/resources. See here for more details.

Try this:

  1. Ensure webapp is servlet 3.0 and index.jsp is a welcome-file

    <?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. Refactor the structure to:

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


来源:https://stackoverflow.com/questions/35784652/embedded-tomcat-spring-file-upload-http-404

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!