Every year in the past, more and more Java frameworks emerged. Like JavaScript, everyone thinks they know what the functionality of a good framework should be like. Even my old grandmother now uses a framework I have never heard of and probably will never use. The bloated framework market is saturated by the bloated framework that can do almost anything, but how to judge it? This article aims to provide the best Java RESTful frameworks available at present. I only introduce lightweight products, skipping those bloated over-designed frameworks. At the same time, I just want them to be stable and mature, providing simple, lightweight features. I've only broken this rule when introducing Play frameworks, the reason is given later. Which Java RESTful framework to use in future projects depends entirely on your current needs. To make your choices easier, I will list the most prominent framework features, hoping this will save you some time.
Dropwizard
Birth time: 2011
Rating: 4.5/5
Dropwizard provides a stable and mature Java library and encapsulates it into a simple and lightweight package.
Dropwizard is somewhere between framework and library. It provides all the necessary needs to develop a web application. Thanks to built-in modularity, an application can remain small and lean, reducing development and maintenance time and reducing burden.
Dropwizard uses the existing Jetty HTTP library to embed into your project without the need for an external server. All Dropwizard projects have a main method to manage built-in HTTP server.
Link <br />Official site GITHUB documentation
advantage
Quick project construction and startup
Modular
Incredible fast (at least based on the results measured by the built-in metric)
Jetty for HTTP, Jersey for REST, and Jackson for JSON
It also supports other libraries, such as Mustache, Logback, JDBI, Hibernate Validator, Guava,…
Support monitoring with Metrics
Main method starts Jetty server, which can be debugged and maintained easily
Strong community strength
shortcoming
Dropwizard documentation is the main source of knowledge, but it is not excellent. You may need to search and discover documents for third-party libraries.
For some reason the error is treated as plain text, if you want the response result to be always JSON, this may be problematic
Make sure to use the latest Dropwizard, some older versions use abandoned third-party libraries. And it was difficult to upgrade early Dropwizzard
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 } } Note <br />I personally don’t want to recommend this framework for large projects. But if you want to try it, you won't be disappointed. Mainly, this framework uses the best modern Java web components and assembles into a simple and easy-to-use framework.
Unfortunately this also brings its problems. Combining these libraries can lead to unforeseen problems. This is why I lost 0.5 stars for it, without rating it as a full score of 5 stars.
Jersey
Birth time: 2012 (Jersey 2.X)
Rating: 5/5
The Jersey RESTful framework is an open source RESTful framework that implements the JAX-RS (JSR 311 & JSR 339) specification. It extends the JAX-RS implementation and provides more features and tools that can further simplify RESTful service and client development. Despite being relatively new to Xinyi, it is already a product-grade RESTful service and client framework.
Link <br />Official site GITHUB documentation
advantage
Excellent documentation and examples
fast
Super easy routing
Smooth JUnit integration
Personally, when developing RESTful service, JAX-RS implementation is better than MVC frameworks.
It can be integrated into other libraries/frameworks (Grizzly, Netty). This may also be the reason why many products use it.
Support asynchronous linking
Don't like servlet containers? You can use them when using Jersey.
WADL, XML/JSON support
Included in Glassfish
shortcoming
Jersey 2.0+ uses some complex dependency injection implementation
Probably not a bad thing. Jersey 1.X implements using older JAX-RS
A large number of third-party libraries only support Jersey 1.X, and are not available in Jersey 2.X
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; }} annotation
Jersey is my choice, 5 stars.
Ninja Web Framework
Birth time: 2012
Rating: 3.5/5
Ninja Web Framework is a full-stack java web framework. Stable, fast, reliable, product-grade.
It provides everything that develops, tests, publishes, and maintains 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 the IDE and start coding.
Link <br />Official site GITHUB documentation
advantage
quick
Quick project construction and startup
Modular
XML, HTML, JSON rendering
Other libraries are also supported (such as 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 and use Jetty as a self-executing jar
shortcoming
Likewise, like DropWizzard, the documentation has but is not good enough. It took me a long time to get to know it. This framework also relies on many other libraries, and sometimes it is troublesome to get the required information.
Not very famous, small community. There are rumors 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); }}Note <br />It looks good, but I'll just throw it aside before it matures.
Play Framework
Birth time: 2011
Rating: 4/5
Use the Play Framework to create, build and publish web applications with Java & Scala support. It uses Akka, based on a lightweight stateless architecture. It should be used for applications with low CPU and memory consumption at a large scale.
Link <br />Official site GITHUB documentation
advantage
Easy to develop
Fast, but not as fast as other frameworks
Based on Netty, it supports non-blocking I/O. It is excellent when handling remote calls in parallel.
The community is big
Quick project construction and startup
Modular
MVC
REST, JSON/XML, Web Sockets, non-blocking I/O
Just refresh the browser to see the latest changes
Support Async
There are published books
shortcoming
Version 2.0 is the most controversial Java framework. Switch to Scala made some Java developers outtraged.
Not backward compatible; Play 2.X rewritten
It is known as lightweight, but it is a bit bloated
SBT build tool. Known as the Maven killer, but never excels in order to replace it. Difficult to learn and configure
Non-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 + "!" ) } }Note <br />No matter how much I complain, I still like and choose this framework first. Unfortunately, I can only give it 4 stars. I firmly believe that a framework based on JAX-RS is more suitable for RESTful web services.
RestExpress
Birth time: 2009
Rating: 3/5
RestExpress is a non-container lightweight Netty HTTP stack wrapper to make it easier to create Java RESTful services.
The goal of RestExpress is to support the best RESTful practices.
Link
GITHUB
advantage
Real microframework
Top performance, fast and reliable
XML/JSON
One of the oldest and most stable RESTful frameworks
shortcoming
No documentation
Almost no support
A very 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(); }}Note <br />Although this framework is super fast, I don't want to recommend it either. The lack of documentation and lack of support make it an underdeveloped framework. Give it 3 stars for speed.
Restlet
Birth time: 2005
Rating: 4.5/5
Restlet helps Java programmers build large-scale, fast web APIs that conform to RESTful architecture patterns.
It provides powerful routing and filtering systems. The unified client/server Java API meets all major platforms (Java SE/EE, Google AppEngine, OSGi, GWT, Android) and provides countless extensions to meet the needs of programmers.
As far as I can tell, it is the first java RESTful web framework. Many companies use it, but you may have never heard of it, as if it is no longer visible.
Link <br />Official site GITHUB documentation
advantage
powerful
Enterprise-level framework
Multi-platform Java SE, Java EE, Google Web Toolkit, Google AppEngine, Android, OSGi environments
Supports JAX-RS (like Jersey)
Most advanced RESTful support
Modular
Support other libraries
Development has been active
Smart url binding, full-featured URI routing
There are related books
shortcoming
Very steep learning curve
Closed community, open even on StackOverflow
No longer popular, more because of 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" ; } }Note <br />Although this framework has been popular all the time, I can't give it 5 stars to its and current completion.
Restx
Birth time: 2013
Rating: 3.5/5
Restx is a lightweight, modular, multi-featured, super fast open source Java REST framework.
Link <br />Official site GITHUB documentation
advantage
Fast, lightweight
Easy to build
Real microframework
Modular
Support other libraries
Support MongoDB
shortcoming
Unfriendly confusing documentation. I expect better documentation for this type of framework
Too young
Asynchronous Async is not supported yet
example
@GET ( "/message/{id}" ) public Message saysHello(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 saysHello(String id, // path param Message msg // body param ) { return msg.setMessage(String.format( "%s @ %s" , msg.getMessage(), DateTime.now().toString( "HH:mm:ss" ))); }Note <br />To be honest, I didn’t spend much time on this framework. It's not worth spending too much effort on another framework, I mean, the Java framework market is becoming more and more fragmented, just like the JavaScript market, this trend should be stopped.
Spark Framework
Birth time: 2011
Rating: 3.5/5
Don't be confused with Apache's big data framework Spark, the Spark framework here 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 minimized kernel of less than 1M and provides all the basic features to build RESTful or traditional web applications.
Link <br />Official site GITHUB documentation
advantage
Fast, lightweight
Excellent rapid prototype
Easy to build
Often used with AngularJS
Real microframework
Using Jetty
Can be used in containers or run independently
shortcoming
Documentation can be better, it is not suitable for beginners
Not suitable for large projects
Community small
example
import static spark.Spark.*; public class HelloWorld { public static void main(String[] args) { get( "/hello" , (req, res) -> "Hello World" ); }}Note <br />This framework is suitable for initial development. Mainly used as small projects or prototypes.