[Introduction to spark in the JAVA framework]

sparkjava is a tiny web framework inspired by Sinatra for fast and rapid creation of web applications in Java.

 

Spark (not to be confused with Apache Spark) is designed to make it easy to create REST APIs or web applications. It's a flexible, concise framework that is only 1MB in size. Spark allows users to choose the template engine for designing the application and choose the library that best suits their project, for example, the HTML parsing function has options such as Freemarker, Mustaches, Velocity, Jade, Handlebars, Pebble or Water, and rarely needs Configuration or boilerplate. However, the price of flexibility and simplicity is that there are fewer user-selectable features. In summary, Spark removes a lot of Java bloat and provides a minimal, flexible web framework. However, due to its high level of simplification, it lacks some functions and is not suitable for the development of large-scale web applications.



 

 

Components in Spark

Routes

In a Spark program, the processing of its requests is done by Route. A Route consists of three parts:

1) A verb, such as get, post, delete, trace, etc.

2) A path, such as "/hello" in the previous example

3) A callback function, such as the handle in the previous example

Another point to note is that when Spark processes requests for path matching, it first matches the Route that appears first. That is, if your request matches multiple Routes, Spark will call the one that appears first to process the request.

 

 

Filters

In addition to Routes, another important component in Spark is Filter. Filter is divided into before filter and after filter. Both of them can obtain Request or modify Response before and after the request is processed by Routes, respectively.

 

 

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?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326491803&siteId=291194637