在tomcat 7上部署Spring Boot War时遇到问题

时间:2018-12-04 12:56:24

标签: java spring maven spring-boot tomcat

在tomcat服务器上部署代码时遇到问题 我已经写了下面的代码。

我的主班:

package com.indiamart.search;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class SuggestMcatApplication  extends SpringBootServletInitializer{

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // TODO Auto-generated method stub
        return builder.sources(SuggestMcatApplication.class);
    }}

我的控制器类:-

package com.abc.search;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class serviceController {

    @RequestMapping("/suggestMcat/related_info")

    public String getSearchString(HttpServletRequest request){
        JSONObject json;
        json =  new JSONObject(request);
        return json.toString();
    }
    }

我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.abc</groupId>
<artifactId>suggestMcat</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>suggestMcat</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.jazzy/jazzy -->
<dependency>
<groupId>net.sf.jazzy</groupId>
<artifactId>jazzy</artifactId>
<version>0.5.2-rtext-1.4.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
    <source>1.8</source>
    <target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
    <outputDirectory>../../tomcat/webapps/</outputDirectory>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    <warName>${project.artifactId}</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
    <execution>
        <id>install-javaml-jar</id>
        <phase>clean</phase>
        <configuration>
            <file>${project.basedir}/src/main/resources/javaml-0.1.6.jar</file>
            <repositoryLayout>default</repositoryLayout>
            <groupId>com.indiamart</groupId>
            <artifactId>javaml</artifactId>
            <version>0.1.6</version>
            <packaging>jar</packaging>
            <generatePom>true</generatePom>
        </configuration>
        <goals>
            <goal>install-file</goal>
        </goals>
    </execution>
    <execution>
        <id>install-ajt-jar</id>
        <phase>clean</phase>
        <configuration>
            <file>${project.basedir}/src/main/resources/ajt-2.11.jar</file>
            <repositoryLayout>default</repositoryLayout>
            <groupId>com.indiamart</groupId>
            <artifactId>ajt</artifactId>
            <version>2.11</version>
            <packaging>jar</packaging>
            <generatePom>true</generatePom>
        </configuration>
        <goals>
            <goal>install-file</goal>
        </goals>
    </execution>
</executions>
</plugin>   
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
</project>

在进行战争并在tomcat服务器上部署时,出现以下错误:

Caused by: java.lang.NoClassDefFoundError: org/springframework/web/filter/FormContentFilter
            at java.base/java.lang.ClassLoader.defineClass1(Native Method) ~[na:na]
            at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1009)
2018-12-04 17:59:39.859  WARN 14727 --- [io-8080-exec-29] o.s.boot.SpringApplication               : Unable to close
     ApplicationContext

         java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration]
     from ClassLoader [WebappClassLoader
           context: /suggestMcat
           delegate: false
           repositories:
             /WEB-INF/classes/
         ---------- Parent Classloader:
         java.net.URLClassLoader@3dd4520b

请提出建议。

答案2之后,我的问题已解决,但部署与控制器有关 白标错误页面 此应用程序没有针对/ error的显式映射,因此您将其视为后备。

IST 2018年12月4日21:36:49 发生意外错误(类型=未找到,状态= 404)。 没有可用消息

2 个答案:

答案 0 :(得分:0)

您的项目引用了类org.springframework.web.filter.FormContentFilter,该类自5.1版以来就存在于spring-web工件中。

因此,您将需要删除对该类的引用或添加缺少的spring-web工件。

答案 1 :(得分:0)

看来您自己建立了一个战争档案。您应该使用spring来做到这一点。

https://www.surasint.com/spring-boot-create-war-for-tomcat/

这里有完整的示例

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>

    <groupId>com.surasint.example</groupId>
    <artifactId>spring-boot-12</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- JSP -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- to deploy as a war in tomcat -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- jstl for jsp -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

关键点是:

<packaging>war</packaging>

和:

<!-- to deploy as a war in tomcat -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>