无法在Eclipse中运行Spring Web Application

时间:2017-01-13 15:40:20

标签: eclipse spring rest spring-mvc tomcat

我关注this rather simple tutorial但由于某种原因,我无法访问我创建的资源/word-embeddings/hello,以便测试它是否在Eclipse中工作。但是,我可以通过运行mvn tomcat7:run从终端运行它,但为什么这不适用于Eclipse?

我没有对教程做过很多更改,所以这里是代码:

AppConfig.java

package org.ema.server.spring.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.ema.server.spring")
public class AppConfig {

}

AppInitializer.java

package org.ema.server.spring.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected Class[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

WordEmbeddingsRestController.java

package org.ema.server.spring.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WordEmbeddingsRestController {


    @GetMapping("/word-embeddings/hello")
    public ResponseEntity<String> getHello() {
        return new ResponseEntity<String>("Hello", HttpStatus.OK);
    }

}

最后但并非最不重要的是 pom.xml

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.ema</groupId>
        <artifactId>ema-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>ema-server</artifactId>
    <name>ema-server</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <springframework.version>4.3.1.RELEASE</springframework.version>
        <jackson.version>2.7.5</jackson.version>
    </properties>

    <dependencies>

        <!-- JUnit -->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring Framework Dependencies -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <!-- Jackson -->

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- JavaX Servlet -->

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>HelloWorld</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <path>/ema</path>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <packaging>war</packaging>
</project>

当我运行Tomcat服务器并尝试访问http://localhost:8080/ema/word-embeddings/hello时,我登陆404页面。

知道为什么会这样吗?

注意:我在AppInitializer中设置了一些断点,以检查这些方法是否被调用,但是,这似乎并非如此,所以看起来整个Spring工具链根本没有被执行。

这是我启动Tomcat 8服务器时收到的完整日志:

Jan 13, 2017 5:26:06 PM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/usr/share/tomcat7/common/classes], exists: [false], isDirectory: [false], canRead: [false]
Jan 13, 2017 5:26:06 PM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/usr/share/tomcat7/common], exists: [false], isDirectory: [false], canRead: [false]
Jan 13, 2017 5:26:06 PM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/usr/share/tomcat7/server/classes], exists: [false], isDirectory: [false], canRead: [false]
Jan 13, 2017 5:26:06 PM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/usr/share/tomcat7/server], exists: [false], isDirectory: [false], canRead: [false]
Jan 13, 2017 5:26:06 PM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/usr/share/tomcat7/shared/classes], exists: [false], isDirectory: [false], canRead: [false]
Jan 13, 2017 5:26:06 PM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/usr/share/tomcat7/shared], exists: [false], isDirectory: [false], canRead: [false]
Jan 13, 2017 5:26:07 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:ema-server' did not find a matching property.
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.68 (Ubuntu)
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jun 27 2016 18:13:17 UTC
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.68.0
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Linux
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            4.4.0-57-generic
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /usr/lib/jvm/java-8-openjdk-amd64/jre
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_111-8u111-b14-2ubuntu0.16.04.2-b14
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /media/Data/workspaces/easy-model-access/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /usr/share/tomcat7
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/media/Data/workspaces/easy-model-access/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/usr/share/tomcat7
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/media/Data/workspaces/easy-model-access/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/usr/share/tomcat7/endorsed
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Jan 13, 2017 5:26:07 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 274 ms
Jan 13, 2017 5:26:07 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 13, 2017 5:26:07 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.68 (Ubuntu)
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jan 13, 2017 5:26:07 PM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jan 13, 2017 5:26:07 PM org.apache.catalina.core.ApplicationContext log
INFO: 1 Spring WebApplicationInitializers detected on classpath
Jan 13, 2017 5:26:07 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Jan 13, 2017 5:26:07 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Jan 13, 2017 5:26:08 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Fri Jan 13 17:26:08 CET 2017]; root of context hierarchy
Jan 13, 2017 5:26:08 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
INFO: Registering annotated classes: [class org.ema.server.spring.config.AppConfig]
Jan 13, 2017 5:26:08 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFO: Mapped "{[/word-embeddings/hello],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.lang.String> org.ema.server.spring.controller.WordEmbeddingsRestController.getHello()
Jan 13, 2017 5:26:08 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Fri Jan 13 17:26:08 CET 2017]; root of context hierarchy
Jan 13, 2017 5:26:08 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 630 ms
Jan 13, 2017 5:26:08 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Jan 13, 2017 5:26:08 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Jan 13, 2017 5:26:08 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Jan 13 17:26:08 CET 2017]; parent: Root WebApplicationContext
Jan 13, 2017 5:26:08 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 18 ms
Jan 13, 2017 5:26:08 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jan 13, 2017 5:26:08 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1389 ms

0 个答案:

没有答案