Let's look at the next simplest example.
1. Add in pom. xml
<Dependency>
<GroupId> com. sparkjava </groupId>
<ArtifactId> spark-core </artifactId>
<Version> 2.0.0 </version>
</Dependency>
2. Create a new Class
Import static spark. Spark .*;
Public class HelloWorld {
Public static void main (String [] args ){
Get ("/hello", (req, res)-> "Hello World ");
}
}
Run HelloWorld directly to access http: // localhost: 4567/hello. The page displays Hello World.
Java can be written so concisely...
II. spark packaging and running
How to enable this lightweight application service? The simplest startup method is
Java-jar ***. jar
In this way, as long as JDK is installed on the server, no other Maven or Tomcat installation is required.
In fact, packaging is very simple. Applications built by Maven can directly use Plugins.
<Plugin>
<GroupId> org. apache. maven. plugins </groupId>
<ArtifactId> maven-compiler-plugin </artifactId>
<Configuration>
<Sources> 1.8 </source>
<Target> 1.8 </target>
<Encoding> UTF-8 </encoding>
</Configuration>
</Plugin>
<Plugin>
<GroupId> org. apache. maven. plugins </groupId>
<ArtifactId> maven-jar-plugin </artifactId>
<Configuration>
<Archive>
<Manifest>
<AddClasspath> true </addClasspath>
<ClasspathPrefix> lib </classpathPrefix>
<MainClass> net. coding. gravatar. Application </mainClass>
</Manifest>
</Archive>
</Configuration>
</Plugin>
<Plugin>
<GroupId> org. apache. maven. plugins </groupId>
<ArtifactId> maven-dependency-plugin </artifactId>
<Executions>
<Execution>
<Id> copy </id>
<Phase> package </phase>
<Goals>
<Goal> copy-dependencies </goal>
</Goals>
<Configuration>
<OutputDirectory>
$ {Project. build. directory}/lib
</OutputDirectory>
</Configuration>
</Execution>
</Executions>
</Plugin>
III. Use cases of Spark
For small services, Spark can accelerate development and have low requirements for building the server environment, as long as you have a JDK environment (note: JDK 8 is required for the JDK Version)