sparckjava初识

sparckjava依赖的jar包极少,使用简单,配置route,指定http方法,指定路径即可建立http服务。

一般可用来做简单web程序或restapi服务,引入(maven)和使用简单(直接使用静态类即可)。

官网是:http://sparkjava.com/

简单程序例子:

pom.xml添加dependency

<dependency>
   <groupId>com.sparkjava</groupId>
   <artifactId>spark-core</artifactId>
   <version>2.2</version>
</dependency>
代码:
package com.mongodb;

import spark.Request;
import spark.Response;
import spark.Route;
import spark.Spark;

public class HelloWorldSparkStyle {

	public static void main(String[] args){
		Spark.get("/",new Route() {
			
			@Override
			public Object handle(Request arg0, Response arg1) throws Exception {
				return "hello world from sparkjava.";
			}
		});
	}
}

可以看到eclipse控制台输出

[Thread-0] INFO spark.webserver.SparkServer - == Spark has ignited ...
[Thread-0] INFO spark.webserver.SparkServer - >> Listening on 0.0.0.0:4567
[Thread-0] INFO org.eclipse.jetty.server.Server - jetty-9.0.2.v20130417
[Thread-0] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector@27375a4e{HTTP/1.1}{0.0.0.0:4567}
[qtp1672789544-21] INFO spark.webserver.MatcherFilter - The requested route [/favicon.ico] has not been mapped in Spark
[qtp1672789544-20] INFO spark.webserver.MatcherFilter - The requested route [/favicon.ico] has not been mapped in Spark
访问url可看输出



你可能感兴趣的:(sparkjava)