EasySpark is a simple and lightweight Java web framework built for quick development. EasySpark uses Spark Framework (http://sparkjava.com/) and add some facilities to organize your code in a MVC architecture.
For a quick start, you can use this snippet in your maven POM:
<dependency>
<groupId>br.com.logiquesistemas</groupId>
<artifactId>easy-spark</artifactId>
<version>1.0.0</version>
</dependency>
First of all create a simple java class with @Controller annotation.
package br.com.logique;
import Controller;
@Controller
public class SimpleController {
public String hello(){
return "Hello World";
}
}
In your main class start a EasySpark calling the setUp method. It is necessary to inform the base package where the engine will look for controllers.
import SparkEngine;
public class App {
public static void main(String[] args) {
SparkEngine engine = new SparkEngine.Builder()
.withBasePackage("br.com.logiquesistemas")
.withPort(7485)
.build();
engine.setUp();
}
}
The base package is the root package where EasySpark will look for controllers.
Now you can access the web page below and see the results
http://localhost:4567/simple/hello
For default, EasySpark create a different URL to each method following the pattern:
http://host:port/controller/method
If necessary you can change default route with @Path annotation.
@Controller
public class SimpleController {
@Path("/hello")
public String hello(){
return "Hello World";
}
}
Now the hello method will be invoked when you access http://localhost:4567/hello
You can also define a global path for all methods inside a controller by using @Path globally
@Controller
@Path("custompath")
public class SimpleController {
@Path("/hello")
public String hello(){
return "Hello World";
}
}
The hello method will now be invoked by the following url: http://localhost:4567/custompath/hello
EasySpark automatically fill user parameter, so it is possible do this:
@Controller
public class SimpleController {
public String hello(String helloMsg){
response.body("Hello");
return helloMsg;
}
}
To test, access http://localhost:4567/simple/hello?helloMsg=Hello!
Also it is possible to use @Path to create friendly URLs, like this:
@Controller
public class SimpleController {
@Path("hello/:helloMsg")
public String hello(String helloMsg){
response.body("Hello");
return helloMsg;
}
}
To test, access http://localhost:4567/hello/Hello!
EasySpark also automatically inject Spark Request and Response instances. Access http://sparkjava.com/documentation.html to more infomations about Request and Response functions. So you can do that:
@Controller
public class SimpleController {
@Path("/hello")
public String hello(Request request, Response response){
String param = request.queryParams("FOO");
response.body("Hello");
return "Hello World";
}
}
To do RESTful web service with EasySpark you just need to put @Get, @Post, @Delete or @Put annotations above methods, like this:
@Controller
public class SimpleController {
@Get
@Path("hello/")
public String helloGet() {
return "Hello World";;
}
@Post
@Path("hello/")
public String helloPost() {
return "Hello World";;
}
@Put
@Path("hello/")
public String helloPut() {
return "Hello World";;
}
@Delete
@Path("hello/")
public String helloDelete() {
return "Hello World";;
}
}
All the paths are the same. So, the Http verb will solve the route conflict.