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

时间:2017-06-03 12:00:01

标签: jsp tomcat servlets spring-boot web.xml

目前,我使用Spring MVC和Spring Boot开发了一个Web应用程序。

我试图将一个弹簧启动应用程序打包为战争。我修改了我的Application类:

@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();
    }

}
  • web.xml version="3.1"
<!-- ====================================================== -->
  <!--  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>

-audit-servlet.xml:

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

</beans>

-I导出我的项目&#34;应用&#34;作为war-file进入&#34; C:\ apache-tomcat-8.5.15 \ webapps \&#34;。 Tomcat创建文件夹&#34; Apps&#34;,其中包含所有内容:

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:类[com.SSC.Services.REST.UtilisateursRestController]不是Servlet    Apache Tomcat / 8.5.15

如何解决这个问题?

谢谢,

1 个答案:

答案 0 :(得分:0)

你的web.xml是这里的罪魁祸首,也是你的依赖关系的第二个竞争者。另请参阅Spring Boot Reference Guide,它在creating a deployable war上有一个很好的部分。您可能还想查看GitHub上的war sample

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

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

<!-- 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这样的东西)您可能需要阅读参考指南中的JSP Limitations部分并查看所需的依赖关系。 GitHub上的JSP sample

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