Tomcat上的Spring RESTful webservice 404

时间:2016-06-18 17:03:21

标签: java spring maven tomcat

我正在尝试在Tomcat服务器实例上部署我的Spring Framework应用程序。对于这两个问题我都是新手。

我的服务器是Google Compute Engine上的x86_64 Debian计算机。我正在使用Tomcat 8.0.36和Java 1.8。

我的问题类似于此主题中描述的问题: Spring RESTful Service as a WAR instead of JAR in Tomcat 我的意思是我得到404"请求的资源不可用。"错误信息。相同的应用程序在我的本地环境中完美运行。

我可能遵循与上述主题中相同的步骤,但我的应用程序仍然无法工作。即使我能够部署WAR文件,我也可以看到应用程序的状态是“正在运行”。在manager / html中,我无法通过http访问它或查看它的日志。

任何人都可以查看代码并帮助我理解原因吗?

主要课程:

package com.kp.quasarrestserver;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;


@SpringBootApplication
@EnableWebMvc
public class QuasarRestServerApplication extends SpringBootServletInitializer{
    private static final Logger log = LoggerFactory.getLogger(QuasarRestServerApplication.class);
    private ApplicationContext applicationContext;


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

    // https://stackoverflow.com/questions/19820029/spring-restful-service-as-a-war-instead-of-jar-in-tomcat
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(QuasarRestServerApplication.class);
    }   

    /*
    // https://spring.io/blog/2014/03/07/deploying-spring-boot-applications 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }

    private static Class<QuasarRestServerApplication> applicationClass = QuasarRestServerApplication.class;
    */

    public void run(String... args) throws Exception {
        applicationContext = new ClassPathXmlApplicationContext("conf.xml");
        com.kp.quasarrestserver.Misc.Configuration config = applicationContext.getBean(com.kp.quasarrestserver.Misc.Configuration.class);
        String c = config.toCustomString(); 
        log.info(String.format("Configuration data: " + c));
        }
}

的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.kp.quasarrestserver</groupId>
    <artifactId>quasarrestserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>QuasarRestServer</name>
    <description>QuasarRestServer Application</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <start-class>com.kp.quasarrestserver.QuasarRestServerApplication</start-class>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <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>       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.53</version>
        </dependency>       
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>                
        </plugins>
    </build>    

</project>

Tomcat日志:

18-Jun-2016 16:57:29.806 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.36
18-Jun-2016 16:57:29.810 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jun 9 2016 13:55:50 UTC
18-Jun-2016 16:57:29.810 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.36.0
18-Jun-2016 16:57:29.810 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
18-Jun-2016 16:57:29.811 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            3.16.0-4-amd64
18-Jun-2016 16:57:29.811 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
18-Jun-2016 16:57:29.811 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /opt/jdk/jdk1.8.0_91/jre
18-Jun-2016 16:57:29.811 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_91-b14
18-Jun-2016 16:57:29.812 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
18-Jun-2016 16:57:29.812 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /opt/tomcat8
18-Jun-2016 16:57:29.812 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /opt/tomcat8
18-Jun-2016 16:57:29.813 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/opt/tomcat8/conf/logging.properties
18-Jun-2016 16:57:29.815 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
18-Jun-2016 16:57:29.815 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
18-Jun-2016 16:57:29.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.endorsed.dirs=/opt/tomcat8/endorsed
18-Jun-2016 16:57:29.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/opt/tomcat8
18-Jun-2016 16:57:29.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/opt/tomcat8
18-Jun-2016 16:57:29.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/opt/tomcat8/temp
18-Jun-2016 16:57:29.817 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/
java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
18-Jun-2016 16:57:30.172 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
18-Jun-2016 16:57:30.205 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
18-Jun-2016 16:57:30.213 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
18-Jun-2016 16:57:30.216 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
18-Jun-2016 16:57:30.216 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1401 ms
18-Jun-2016 16:57:30.267 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
18-Jun-2016 16:57:30.271 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.36
18-Jun-2016 16:57:30.315 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /opt/tomcat8/webapps/QuasarRestServer.war
18-Jun-2016 16:57:31.076 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /opt/tomcat8/webapps/QuasarRestServer.war has finished in 756 ms
18-Jun-2016 16:57:31.077 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/tomcat8/webapps/host-manager
18-Jun-2016 16:57:31.143 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /opt/tomcat8/webapps/host-manager has finished in 66 ms
18-Jun-2016 16:57:31.146 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/tomcat8/webapps/ROOT
18-Jun-2016 16:57:31.182 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /opt/tomcat8/webapps/ROOT has finished in 36 ms
18-Jun-2016 16:57:31.183 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/tomcat8/webapps/docs
18-Jun-2016 16:57:31.224 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /opt/tomcat8/webapps/docs has finished in 41 ms
18-Jun-2016 16:57:31.225 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/tomcat8/webapps/manager
18-Jun-2016 16:57:31.422 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /opt/tomcat8/webapps/manager has finished in 197 ms
18-Jun-2016 16:57:31.425 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/tomcat8/webapps/examples
18-Jun-2016 16:57:31.950 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /opt/tomcat8/webapps/examples has finished in 525 ms
18-Jun-2016 16:57:31.954 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
18-Jun-2016 16:57:31.968 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
18-Jun-2016 16:57:31.973 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1756 ms

1 个答案:

答案 0 :(得分:0)

@EnableWebMvc未在配置中设置,它在控制器上设置。

在配置(conf.xml)中设置它。