【JAVA框架之spark介绍】

sparkjava是一个受Sinatra启发的微型Web框架,用于采用Java快速快速创建Web应用。

Spark(注意不要同Apache Spark混淆)的设计初衷是,可以简单容易地创建REST API或Web应用程序。它是一个灵活、简洁的框架,大小只有1MB。Spark允许用户自己选择设计应用程序的模板引擎以及选择最适合他们项目的库,比如,HTML解析功能就有Freemarker、Mustaches、Velocity、Jade、Handlebars、Pebble或Water等选项可供选择,而且很少需要配置或样板文件。不过,灵活简单的代价是,用户可选的功能减少。总之,Spark剔除了许多Java的臃肿之物,提供了一个最小化的、灵活的Web框架。但由于精简程度较高,它缺少了一些功能,不适合用于大型Web应用程序的开发。



 

Spark中的组件

Routes

在Spark程序中,其请求的处理都是由Route来完成的,一个Route由三部分组成:

1)一个动词,比如get,post,delete,trace等等

2)一个路径,比如前面的例子中的“/hello”

3)回调函数,比如前面的例子中的handle

另外需要注意的一点是,Spark在处理请求进行路径匹配的时候是优先匹配先出现的Route,也就是如果你的请求匹配到了多个Route,那么Spark会调用先出现的那个来处理请求。

Filters

除了Routes之外,Spark中另一个重要的组件就是Filter,filter分为before filter和after filter,两者分别可以在请求被Routes处理之前和被Routes处理之后获取Request或者对Response进行修改,

Built for productivity

Spark Framework is a simple and expressive Java/Kotlin web framework DSL built for rapid development. Sparks intention is to provide an alternative for Kotlin/Java developers that want to develop their web applications as expressive as possible and with minimal boilerplate. With a clear philosophy Spark is designed not only to make you more productive, but also to make your code better under the influence of Spark’s sleek, declarative and expressive syntax.

Lets you take full advantage of the JVM

The JVM offers one of the biggest programming ecosystems in the world. It has a lot of Java web frameworks, but pure Java web development has traditionally been very cumbersome. If you love the JVM, but hate verbose code and frameworks, Spark is the web framework for you. It will have you up and running in minutes, and you can even use it with Groovy or Kotlin or whatever you want. Spark is an expressive, lightweight and unopinionated pure Java (and Kotlin) web framework that doesn’t get in your way, unlike in other web frameworks, you can structure your application as you want.

Microservices, microservices everywhere!

2015 was the year of microservice hype, and now that you’re getting around to looking into it you’re about to realize that Spark is great for microservices. Microservices work best with micro frameworks, and Spark has your REST API ready to serve JSON in less than ten lines of code. Spark is mainly used for creating REST API’s, but it also supports a multitude of template engines. Why not create one Spark application for your backend and one for your frontend?

猜你喜欢

转载自gaojingsong.iteye.com/blog/2378647