The best 8 Java RESTful frameworks
- 2020-05-09 18:42:24
- OfStack
With each 1year in the past, more and more Java frameworks have emerged. Like JavaScript, everyone thinks they know what a good framework should look like. Even my old grandmother now USES a framework I've never heard of and probably never will. Jokes aside, the bloated framework market, which can do almost anything, is saturated, but how to judge. This article aims to provide the best Java RESTfulful framework available today. I'm just going to talk about lightweight products, skipping the bloated over-designed frameworks. At the same time, I just want them to be stable and mature, offering simple, lightweight features. I only break this rule when I introduce Play frameworks, for reasons I'll give later. Which Java RESTful framework to use in future projects depends entirely on your current requirements. To make it easier for you to choose, I'll list the most prominent framework features, which I hope will save you some time.
Dropwizard
Date of birth: 2011
4.5/5 grade:
Dropwizard provides a stable and mature Java library wrapped in a simple, lightweight package.
Dropwizard is somewhere between a framework and a library. It provides all you need to develop an web application. Thanks to built-in modularity, an application can remain small and lean, reducing development and maintenance time and overhead.
Dropwizard USES the existing Jetty HTTP library and is embedded in your project without the need for an external server. All Dropwizard projects have an main method to manage the built-in HTTP server.
link
Official site GITHUB documentation
advantages
Rapid project build and launch
modular
Incredibly fast (at least as measured by the built-in metric)
Jetty for HTTP, Jersey for REST, and Jackson for JSON
Other libraries are also supported, such as Mustache, Logback, JDBI, Hibernate Validator, Guava...
Monitoring is supported using Metrics
The Main method starts Jetty server and can be easily debugged and maintained
Strong community
disadvantages
The Dropwizard documentation is a major source of knowledge, but it's not great. You may need to search and explore the documentation of the third party library.
The error is treated as plain text for some reason, which can be problematic if you want the response result to always be JSON
Be sure to use the latest version of Dropwizard. Some older versions use an obsolete third party library. And the early Dropwizzard was hard to upgrade
example
package com.example.helloworld;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import com.example.helloworld.resources.HelloWorldResource;
import com.example.helloworld.health.TemplateHealthCheck;
public class HelloWorldApplication extends Application<HelloWorldConfiguration> {
public static void main(String[] args) throws Exception {
new HelloWorldApplication().run(args);
}
@Override
public String getName() {
return "hello-world" ;
}
@Override
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
// nothing to do yet
}
@Override
public void run(HelloWorldConfiguration configuration,
Environment environment) {
// nothing to do yet
}
}
annotations
Individuals do not want to recommend this framework for large projects. But if you try, you won't be disappointed. Primarily this framework USES the best modern Java web components, assembled into an easy-to-use framework.
Unfortunately this brings its own problems. Mashing these libraries together can cause unforeseen problems. That's why I subtracted 0.5 stars from it instead of 5 stars out of 10.
Jersey
Date of birth: 2012 (Jersey 2.X)
5/5 grade:
The Jersey RESTful framework is an open source RESTful framework that implements JAX-RS (JSR 311) & JSR 339) specification. It extends the JAX-RS implementation with additional features and tools to further simplify RESTful service and client development. Although relatively new, it is already a product of the RESTful service and client frameworks.
link
Official site GITHUB documentation
advantages
Excellent documentation and examples
fast
Super easy routing
Smooth JUnit integration
Personally, when developing RESTful service, the JAX-RS implementation is better than the MVC framework.
It can be integrated into other libraries/frameworks (Grizzly, Netty). This is probably why many products use it.
Support for asynchronous linking
Don't like servlet container; You can use Jersey without them.
WADL, XML/JSON support
Included in Glassfish
disadvantages
Jersey 2.0+ USES a somewhat complex implementation of dependency injection
Maybe not a bad thing. Jersey 1.X USES the older JAX-RS implementation
1 heap 3 libraries only support Jersey 1.X, Jersey 2.X is not available
example
package org.glassfish.jersey.examples.helloworld;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path ( "helloworld" )
public class HelloWorldResource {
public static final String CLICHED_MESSAGE = "Hello World!" ;
@GET
@Produces ( "text/plain" )
public String getHello() {
return CLICHED_MESSAGE;
}
}
annotations
Jersey is my choice, 5 stars.
Ninja Web Framework
Date of birth: 2012
3.5/5 grade:
Ninja Web Framework is the full stack of java web framework. Stable, fast, reliable, product grade.
It provides a slice of development, testing, publishing, and maintenance of RESTful web applications (Servlets, Guice, JPA, Flyway migrations, Maven, etc.).
Just like DropWizzard, Ninja Web Framework is an integrated software stack. You don't have to build your own, just use Maven archetype to generate a new project, import it into IDE and start coding.
link
Official site GITHUB documentation
advantages
fast
Rapid project build and launch
modular
XML, HTML, JSON render
Other libraries are also supported (e.g., Guice, Logback, Guava, etc.)
Good data persistence and caching
Don't like servlet container; U can choose the container you like
If you don't like containers at all, you can use standalone mode, using Jetty as a self-executing jar
disadvantages
Again, like DropWizzard, the documentation is there but not good enough. It took me a long time to understand it. The framework also relies on many other libraries, and sometimes it can be difficult to get the information you need.
Not very well known. Small community. Rumor has it that this framework was created by Play 2.X users who switched to Scala
example
package controllers;
public class ApplicationController {
public Result index() {
Person person = new Person();
person.name = "John Johnson" ;
return Results.json().render(person);
}
}
annotations
It looks good, but I'm going to leave it on the one side until it matures.
Play Framework
Date of birth: 2011
4/5 grade:
Play Framework is easy to create, build, and publish web applications with Java support & Scala. It USES Akka, based on a lightweight stateless architecture. It should be used for large-scale applications with low CPU and memory consumption.
link
Official site GITHUB documentation
advantages
Easy to develop
Fast, but not as fast as some other frames
Support for non-blocking I/O based on Netty. Excellent for handling remote calls in parallel
The community is very big
Rapid project build and launch
modular
MVC
REST, JSON/XML, Web Sockets, non-blocking I/O
Just refresh your browser to see the latest changes
Support Async
There are published books
disadvantages
Version 2.0 is the most controversial Java framework. Switch to Switch to Scala made some Java developers outraged.
Backward incompatible; Play 2.X has been rewritten
Known as lightweight, but a bit bloated
The SBT build tool. Known as the Maven killer, but never good enough to replace it. Difficult to learn and configure
The servlet
Breaking changes across releases
example
package controllers
import play.api._
import play.api.mvc._
class Application extends Controller {
def hello(name: String) = Action {
Ok( "Hello " + name + "!" )
}
}
annotations
For all the complaining, I still prefer 1 straight and preferred this framework. Unfortunately, I can only give it four stars. I firmly believe that the framework based on JAX-RS is more suitable for RESTful web services.
RestExpress
Date of birth: 2009
3/5 grade:
RestExpress is a non-container lightweight Netty HTTP stack wrapper to make it easier to create Java RESTful services.
The RestExpress goal is to support the best RESTful practices.
link
GITHUB
advantages
Real micro frames
Top performance, fast, reliable
XML/JSON
The oldest and most stable RESTful framework 1
disadvantages
No document
Little support
Small community
example
package com.example;
import java.io.IOException;
import io.netty.handler.codec.http.HttpMethod;
import org.restexpress.RestExpress;
public class Main
{
public static RestExpress startServer(String[] args) throws IOException
{
RestExpress server = new RestExpress();
MyResource r = new MyResource();
server.uri( "/myapp/myresource" , r)
.method(HttpMethod.GET)
.noSerialization();
server.uri( "/myapp/myresource" , r)
.method(HttpMethod.POST);
server.bind( 8080 );
return server;
}
public static void main(String[] args) throws Exception
{
RestExpress server = startServer(args);
System.out.println( "Hit enter to stop it..." );
System.in.read();
server.shutdown();
}
}
annotations
Even though this framework is super fast, I don't want to recommend it. Lack of documentation and no support makes it a poor framework. Give it three stars for speed's sake.
Restlet
Date of birth: 2005
4.5/5 grade:
Restlet helps Java programmers build large-scale, fast web api architectures that conform to the RESTful architectural pattern.
It provides powerful routing and filtering systems. client/server Java API. Meets all the major platforms (Java SE/EE, Google AppEngine, OSGi, GWT, Android) and provides countless extensions to meet the needs of programmers.
As far as I know, it is the first java RESTful web framework. A lot of companies use it, but you've probably never heard of it, as if it's no longer visible.
link
Official site GITHUB documentation
advantages
strong
Enterprise level framework
Multi-platform Java SE, Java EE, Google Web Toolkit, Google AppEngine, Android, OSGi environments
Support JAX-RS (like Jersey)
Most advanced RESTful support
modular
Support for other libraries
Development 1 is active
Intelligent url binding, fully functional URI routing
There are books about it
disadvantages
Very steep learning curve
Closed communities, although StackOverflow is still open
No longer popular, more due to Play Framework and Jersey
example
public class Part03 extends ServerResource {
public static void main(String[] args) throws Exception {
// Create the HTTP server and listen on port 8182
new Server(Protocol.HTTP, 8182 , Part03. class ).start();
}
@Get ( "txt" )
public String toString() {
return "hello, world" ;
}
}
annotations
Although the framework is still 1 in popularity, I can't give it 5 stars to its current degree of completion.
Restx
Date of birth: 2013
3.5/5 grade:
Restx is a lightweight, modular, feature-rich, super-fast open source Java REST framework.
link
Official site GITHUB documentation
advantages
Fast, lightweight
Easy to set up
Real micro frames
modular
Support for other libraries
Support MongoDB
disadvantages
Unfriendly confusing document. I expect a good 1 point of documentation for this type of framework
Too young
Asynchronous Async is not currently supported
example
@GET ( "/message/{id}" )
public Message sayHello(String id, // path param
String who // query param
) {
return new Message().setMessage(String.format(
"hello %s, it's %s" ,
who, DateTime.now().toString( "HH:mm:ss" )));
}
@POST ( "/message/{id}" )
public Message sayHello(String id, // path param
Message msg // body param
) {
return msg.setMessage(String.format(
"%s @ %s" ,
msg.getMessage(), DateTime.now().toString( "HH:mm:ss" )));
}
annotations
Honestly, I didn't spend much time on this framework. I mean, the Java framework market is getting more and more fragmented, just like the JavaScript market, and it should stop.
Spark Framework
Date of birth: 2011
3.5/5 grade:
Not to be confused with Apache's big data framework Spark, the Spark framework is a lightweight Java web framework for rapid development (50% of Spark users use Spark to create REST APIs). It is inspired by the Ruby framework Sinatra.
It has a minimal kernel of less than 1M, providing all the basic features for building RESTful or traditional web applications.
link
Official site GITHUB documentation
advantages
Fast, lightweight
Excellent rapid prototyping
Easy to set up
Often used with AngularJS
Real micro frames
Using Jetty
It can be used in a container or run independently
disadvantages
The documentation could be better, it's not for beginners
Not suitable for large projects
Small community
example
import static spark.Spark.*;
public class HelloWorld {
public static void main(String[] args) {
get( "/hello" , (req, res) -> "Hello World" );
}
}
annotations
This framework is suitable for initial development. Mainly used for small projects or prototypes.