每当我尝试运行.war文件时,我都会在此Wiki上学习SpringBoot,它给出了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));
}
}
应用程序
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);
}
}
我所有的Jar都包含Springframework5.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]
最佳答案
Spring Boot旨在作为可执行JAR文件运行(因此public static void main(String[] args)
存在。SpringBoot随后将启动嵌入式Web服务器并处理其余部分。如果要部署Spring Boot应用程序,则需要添加一些支持类来实现这一点。请查看说明here.
关于java - Spring Boot应用程序无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48289652/