Apache Tomcat-javax.servlet.ServletException:我的 Class 不是 Servlet

Apache Tomcat-javax.servlet.ServletException: My Class is not a Servlet

目前我正在使用 Spring MVC 和 Spring Boot 开发 Web 应用程序。

我试图将 spring 启动应用程序打包为 war。我修改了我的申请 class:

@SpringBootApplication  
@ComponentScan
@EnableAutoConfiguration
public class AuditConfigurationApplication extends SpringBootServletInitializer {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(AuditConfigurationApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(AuditConfigurationApplication.class, args);
    }

-pom.xml 文件:

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency> 

        <!-- spring data -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

         <!-- pdfbox -->
        <dependency>
           <groupId>org.apache.pdfbox</groupId>
           <artifactId>pdfbox</artifactId>
           <version>2.0.1</version>
          </dependency>

   <!-- itextpdf -->
       <dependency>
           <groupId>com.itextpdf</groupId>
           <artifactId>itextpdf</artifactId>
           <version>5.0.6</version>
        </dependency>

     <dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
    </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
         </dependency>

    <!-- mysql-connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

    <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <version>8.5.11</version>
</dependency>

      <!-- Spring security  -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.0.3.RELEASE</version>
         </dependency>

         <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.0.3.RELEASE</version>
         </dependency>

         <dependency>
             <groupId>org.springframework.security</groupId>
             <artifactId>spring-security-web</artifactId>
            <version>4.0.3.RELEASE</version>
       </dependency>

        <!-- ecj for exception "No Java compiler available"  -->
       <dependency>
             <groupId>org.eclipse.jdt.core.compiler</groupId>
             <artifactId>ecj</artifactId>
            <version>4.6.1</version>
            <scope>provided</scope>
      </dependency>

        <!-- for uri:  -->
     <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>

     <dependency>
       <groupId>javax.servlet.jsp.jstl</groupId>
         <artifactId>jstl-api</artifactId>
         <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
       <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
   <dependency>
     <groupId>javax.servlet.jsp</groupId>
     <artifactId>jsp-api</artifactId>
     <version>2.2</version>
     <scope>provided</scope>
  </dependency>

        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
     </dependency>     
   <!-- For JSON Object -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1</version>
</dependency>

</dependencies>

-我的控制器休息:

package com.SSC.Services.REST;

@CrossOrigin
@RestController
@RequestMapping("/Employees")
public class UtilisateursRestController {

@RequestMapping(value="/ALL", method=RequestMethod.GET)
    public List<Users> AllUsers(){
    return UM.listeT();
    }

}
<!-- ====================================================== -->
  <!--  2. Define servlet with private context               -->
  <!-- ===================================================== -->
  <servlet>
    <servlet-name>audit</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- ===================================================== -->
  <!-- One servlet, the audit, to rule it all           -->
  <!-- ===================================================== -->
  <servlet-mapping>
    <servlet-name>audit</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

 <servlet>
        <servlet-name>Users</servlet-name>
        <servlet-class>com.SSC.Services.REST.UtilisateursRestController</servlet-class>
    </servlet> 
    <servlet-mapping>
        <servlet-name>Users</servlet-name>
        <url-pattern>/Employees/*</url-pattern> //URL of controller REST
    </servlet-mapping>

<error-page>
 <error-code>404</error-code>
 <location>/404</location>
</error-page>

-审计-servlet.xml :

<beans>
<!-- register controller in servlet private context -->
  <context:component-scan base-package="com.SSC.Services.REST"/>  

</beans>

-我将我的项目 "Apps" 作为 war 文件导出到 "C:\apache-tomcat-8.5.15\webapps\"。 Tomcat 创建文件夹 "Apps",其中包含所有内容:

Apps
  --META-INF
     -maven
     -MANIFEST.MF
     -war-tracker
  --WEB-INF
      -classes
           -com
           -static
           -application.properties
      - lib-provided
      -lib
      - web.xml + audit-servlet.xml
  --org
  -- My JSP pages ..

当我访问 API REST: http://95.x.x.x/Apps(Name of floder)/Employees/ALL 时,出现以下错误消息:

javax.servlet.ServletException: Class [com.SSC.Services.REST.UtilisateursRestController] is not a Servlet Apache Tomcat / 8.5.15

如何解决这个问题?

谢谢,

你的 web.xml 是这里的罪魁祸首,作为第二个竞争者你的依赖。另请参阅 Spring 引导参考指南,该指南在 GitHub 上也有关于 creating a deployable war. You might want to take a look at the war sample 的精彩部分。

首先删除您的 web.xml 您不需要它。您的 AuditConfigurationApplication 也是 SpringBootServletInitializer,它负责在 servlet 容器中引导应用程序(而不是可执行)。

接下来你的依赖关系也有点不稳定。您应该使用 spring-boot-starter-security 而不是手动定义 Spring 安全依赖项。并且您应该为嵌入式库使用另一个依赖项。

<!-- Spring security  -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

您应该提供以下内容

<!-- Provided (for embedded war support) -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

根据您的依赖关系判断您也在使用 JSP(尽管我现在建议使用 Thymeleaf 之类的东西)您可能想阅读 GitHub 上的 JSP Limitations section in the reference guide and take a look at the needed dependencies in the JSP sample

您不需要 servlet-api 已包含在 spring-boot-starter-web 中的依赖项。