Spring Boot应用程序无法运行

时间:2018-01-16 20:53:21

标签: java spring rest tomcat spring-boot

我正在关注此wiki以了解SpringBoot,每当我尝试运行.war文件时,它都会给出404错误。这是我在我的系统上创建的文件。

的build.xml

<?xml version="1.0"?>

    <project name="HelloSpringBoot" basedir="." default="usage">
    <property file="build.properties"/>

    <property name="src.dir" value="src"/>
    <property name="web.dir" value="war"/>
    <property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
    <property name="test.dir" value="test"/>
    <property name="name" value="HelloSpringBoot"/>

    <path id="master-classpath">
        <fileset dir="${web.dir}/WEB-INF/lib">
            <include name="*.jar"/>
        </fileset>
        <!-- We need the servlet API classes: -->
        <!--  * for Tomcat 5/6 use servlet-api.jar -->
        <!--  * for other app servers - check the docs -->
        <fileset dir="${appserver.lib}">
            <include name="servlet*.jar"/>
        </fileset>
        <pathelement path="${build.dir}"/>
    </path>

    <target name="usage">
        <echo message=""/>
        <echo message="${name} build file"/>
        <echo message="-----------------------------------"/>
        <echo message=""/>
        <echo message="Available targets are:"/>
        <echo message=""/>
        <echo message="build     --> Build the application"/>
        <echo message="deploy    --> Deploy application as directory"/>
        <echo message="deploywar --> Deploy application as a WAR file"/>
        <echo message="start-webapp   --> Start application in Tomcat"/>
        <echo message="stop-webapp    --> Stop application in Tomcat"/>
        <echo message="deploy-webapp     --> Deploy application in Tomcat"/>
        <echo message="undeploy-webapp      --> Undeploy application in Tomcat"/>
        <echo message="list      --> List Tomcat applications"/>
        <echo message=""/>
    </target>

    <target name="build" description="Compile main source tree java files">
        <mkdir dir="${build.dir}"/>
        <javac destdir="${build.dir}"  debug="true"
               deprecation="false" optimize="false" failonerror="true">
            <src path="${src.dir}"/>
            <classpath refid="master-classpath"/>
        </javac>
    </target>

    <target name="buildtests" description="Building All TestCases">
        <mkdir dir="${build.dir}"/>
        <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true"
               deprecation="false" optimize="false" failonerror="true">
            <src path="${test.dir}"/>
            <classpath refid="master-classpath"/>
        </javac>
    </target>

    <target name="clean" description="Cleaning All build files">
        <delete>
            <fileset dir="${build.dir}">
                <include name="**/*.class"/>
            </fileset>
        </delete>
    </target>

    <target name="tests" depends="build,buildtests" description="Running All TestCases">
        <junit printsummary="on" fork="false" haltonfailure="false" failureproperty="tests.failed"
               showoutput="true">
            <classpath refid="master-classpath"/>
            <formatter type="brief" usefile="false"/>

            <batchtest>
                <fileset dir="${build.dir}">
                    <include name="**/*Tests.*"/>
                </fileset>
            </batchtest>
        </junit>

        <fail if="tests.failed">
            tests.failed=${tests.failed}
            ***********************************************************
            ***********************************************************
            ****  One or more tests failed!  Check the output ...  ****
            ***********************************************************
            ***********************************************************
        </fail>
    </target>

    <target name="deploy" depends="build" description="Deploy application">
        <copy todir="${deploy.path}/${name}" preservelastmodified="true">
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>
            </fileset>
        </copy>
    </target>

    <target name="deploywar" depends="build" description="Deploy application as a WAR file">
        <war destfile="${name}.war"
             needxmlfile="false">
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>
            </fileset>
        </war>
        <copy todir="${deploy.path}" preservelastmodified="true">
            <fileset dir=".">
                <include name="*.war"/>
            </fileset>
        </copy>
    </target>

    <!-- ============================================================== -->
    <!-- Tomcat tasks - remove these if you don't have Tomcat installed -->
    <!-- ============================================================== -->

    <path id="catalina-ant-classpath">
        <!-- We need the Catalina jars for Tomcat -->
        <!--  * for other app servers - check the docs -->
        <fileset dir="${appserver.lib}">
            <include name="catalina-ant.jar"/>
            <include name="tomcat-coyote.jar"/>
            <include name="tomcat-util.jar"/>
        </fileset>
        <fileset dir="${appserver.home}/bin">
            <include name="tomcat-juli.jar"/>
        </fileset>
    </path>

    <taskdef name="catalina-deploy" classname="org.apache.catalina.ant.DeployTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-list" classname="org.apache.catalina.ant.ListTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-reload" classname="org.apache.catalina.ant.ReloadTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-findleaks" classname="org.apache.catalina.ant.FindLeaksTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-resources" classname="org.apache.catalina.ant.ResourcesTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-start" classname="org.apache.catalina.ant.StartTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-stop" classname="org.apache.catalina.ant.StopTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-undeploy" classname="org.apache.catalina.ant.UndeployTask" classpathref="catalina-ant-classpath"/>

    <target name="start-webapp" description="Start application in Tomcat">
        <catalina-start url="${tomcat.manager.url}"
                        username="${tomcat.manager.username}"
                        password="${tomcat.manager.password}"
                        path="/${name}" />
    </target>

    <target name="stop-webapp" description="Stop application in Tomcat">
        <catalina-stop url="${tomcat.manager.url}"
                 username="${tomcat.manager.username}"
                 password="${tomcat.manager.password}"
                 path="/${name}" />
    </target>

    <target name="deploy-webapp" description="Deploy application in Tomcat">
        <catalina-deploy url="${tomcat.manager.url}"
                username="${tomcat.manager.username}"
                password="${tomcat.manager.password}"
                path="/${name}"
                war="file:${deploy.path}/${name}.war"/>
    </target>

    <target name="undeploy-webapp" description="Undeploy Tomcat application">
        <catalina-undeploy url="${tomcat.manager.url}"
               username="${tomcat.manager.username}"
               password="${tomcat.manager.password}"
               path="/${name}"/>
    </target>

    <target name="list" description="List Tomcat applications">
        <catalina-list url="${tomcat.manager.url}"
              username="${tomcat.manager.username}"
              password="${tomcat.manager.password}"/>
    </target>

    <target name="reload" description="Reload Application in Tomcat">
        <catalina-reload url="${tomcat.manager.url}"
                         username="${tomcat.manager.username}"
                         password="${tomcat.manager.password}"
                         path="/${name}"/>
    </target>

    <!-- End Tomcat tasks -->

    </project>

Greetings.java

package main.java.POJO;


    public class Greetings {
        private final long id;
        private final String name;

        public Greetings(final long id, final String name) {
            this.id = id;
            this.name = name;
        }

        public long getId(final long id) {
            return this.id;
        }

        public String getName(final long name) {
            return this.name;
        }
    }

GreetingController

package main.java.Controller;

    import main.java.POJO.Greetings;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    import java.util.concurrent.atomic.AtomicLong;



    @RestController
    public class GreetingController {
        private static final String template = "Hello %s";
        private final AtomicLong atomicLong = new AtomicLong();

        @RequestMapping("/greeting")
        public Greetings greeting(@RequestParam(value = "name", defaultValue = "World")String name) {
            return new Greetings(atomicLong.incrementAndGet(), String.format(template, name));
        }
    }

Application.Java

package main.java.Application;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication(scanBasePackages = {"src.main.java.Controller", "src.main.java.POJO"})
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

我有所有的jarframework5.0.2和SpringBoot-1.5.9以及SpringBoot-AutoConfig-1.5.9,jackson-core和jackson-all,servlet,junit

请帮我解决此问题。

编辑:   我在我的控制台上运行此命令:

$ ant build deploy deploywar reload
  Buildfile: /Users/Blah/Projects/SpringLearning/HelloSpringBoot/build.xml

 build:
[javac] /Users/Blah/Projects/SpringLearning/HelloSpringBoot/build.xml:46: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file to /Users/Blah/Projects/SpringLearning/HelloSpringBoot/war/WEB-INF/classes

build:
[javac] /Users/Blah/Projects/SpringLearning/HelloSpringBoot/build.xml:46: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

  deploy:
 [copy] Copying 76 files to /usr/local/Cellar/tomcat/8.5.24/libexec/webapps/HelloSpringBoot

build:
[javac] /Users/Blah/Projects/SpringLearning/HelloSpringBoot/build.xml:46: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

  deploywar:
  [war] Building war: /Users/Blah/Projects/SpringLearning/HelloSpringBoot/HelloSpringBoot.war
 [copy] Copying 1 file to /usr/local/Cellar/tomcat/8.5.24/libexec/webapps
 reload:
 [catalina-reload] OK - Reloaded application at context path [/HelloSpringBoot]

2 个答案:

答案 0 :(得分:0)

Spring Boot旨在作为可执行的JAR文件运行(因此存在public static void main(String[] args)。然后,Spring Boot将启动嵌入式Web服务器并处理其余的。如果要部署Spring Boot应用程序,需要添加一些支持类来实现这一点。请查看说明here.

答案 1 :(得分:0)

@SpringBootApplication(scanBasePackages = {"src.main.java.Controller", "src.main.java.POJO"})

基础包对我来说不合适,似乎你的课程在:

package main.java.Controller;package main.java.Application;

您是否有任何理由希望维护复杂的Ant文件和Spring Boot依赖项,您还必须下载所有可传递的文件,而不是使用Maven或{{ 1}}?