Home  >  Article  >  Java  >  The 8 Best Java RESTful Frameworks

The 8 Best Java RESTful Frameworks

高洛峰
高洛峰Original
2017-01-05 12:26:58926browse

Every year in the past, more and more Java frameworks have emerged. Just like JavaScript, everyone thinks they know how a good framework should function. Even my grandma now uses a framework that I have never heard of and will probably never use. All kidding aside, the market is saturated with bloated frameworks that can do almost anything, but how do you judge? This article aims to provide the best Java RESTful frameworks out there. I only introduce lightweight products and skip those bloated and 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 introducing Play frameworks, for reasons given later. Which Java RESTful framework to use in future projects depends entirely on your current needs. To make it easier for you to choose, I’ll list the most prominent framework features, hopefully this will save you some time.

Dropwizard

Date of birth: 2011
Rating: 4.5/5

Dropwizard provides a stable and mature Java library and encapsulates it into a simple and lightweight Bag.
Dropwizard is between a framework and a library. It provides everything needed to develop a web application. Thanks to built-in modularity, an application can stay small and lean, reducing development and maintenance time and burden.
Dropwizard uses the existing Jetty HTTP library and is embedded into your project without the need for an external server. All Dropwizard projects have a main method to manage the built-in HTTP server.

Link
Official Site GITHUB Documentation

Advantages

Fast project construction and startup

Modular

Incredibly fast (at least according to the built-in metrics)

Jetty for HTTP, Jersey for REST, and Jackson for JSON

Also supports other libraries, such as Mustache, Logback, JDBI, Hibernate Validator, Guava, …

Use Metrics to support monitoring

Main method starts the Jetty server, which can be easily debugged and maintained

Strong community

Disadvantages

Dropwizard documentation is the main source of knowledge, but it is not excellent. You may need to search and explore the documentation for third-party libraries.

For some reason the error is treated as normal text, this may be problematic if you want the response result to always be JSON

Make sure to use the latest Dropwizard, some older versions use the deprecated Three-party library. Moreover, it is difficult to upgrade the 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
I personally do not 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, assembled into a simple and easy-to-use framework.
Unfortunately this also brings its own problems. Combining these libraries can lead to unforeseen problems. This is why I deducted 0.5 stars from it instead of giving it a full 5 stars.

Jersey

Date of birth: 2012 (Jersey 2.X)
Rating: 5/5

Jersey RESTful framework is an open source RESTful framework that implements JAX- RS (JSR 311 & JSR 339) specifications. It extends the JAX-RS implementation and provides more features and tools to further simplify RESTful service and client development. Although relatively new, it is already a production-grade RESTful service and client framework.

Link
Official Site GITHUB Documentation

Advantages

Excellent documentation and examples

Quick

Super easy routing

Smooth JUnit integration

Personally, when developing RESTful services, the JAX-RS implementation is better than the MVC framework.

Can be integrated into other libraries/frameworks (Grizzly, Netty). This may also be the reason why many products use it.

Support asynchronous connections

Don’t like servlet containers? You don’t need them when using Jersey.

WADL, XML/JSON support

Included in Glassfish

Disadvantages

Jersey 2.0+ uses a somewhat complex dependency injection implementation

is probably not a bad thing. Jersey 1.X uses the older JAX-RS implementation

A lot 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;
   }
}

Note
Jersey is my choice, 5 stars.

Ninja Web Framework

Date of birth: 2012
Rating: 3.5/5
Ninja Web Framework is a full-stack java web framework. Stable, fast, reliable, production-grade.
It provides everything to develop, test, publish, and maintain 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 the Maven archetype to generate a new project, import it into the IDE and start coding.

Link
Official Site GITHUB Documentation

Advantages

Fast

Quick project construction and startup

Modularization

XML, HTML, JSON rendering

Also supports other libraries (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 the container at all, you can use standalone mode and use Jetty as a self-executing jar

Disadvantages

同样,就像DropWizzard, 文档有但是不够好。我花了好长时间去了解它。这个框架也依赖很多其它的库,有时候想得到所需的信息很麻烦。

不怎么出名,社区小。 有谣言说这个框架是由那些切换到Scala的 Play 2.X 用户创建的

例子

package controllers;
  
public class ApplicationController {   
  
   public Result index() {
  
     Person person = new Person();
     person.name = "John Johnson" ;
  
     return Results.json().render(person);
  
   }
}

注解
看起来不错,但是在它成熟之前我还是把它丢在一边吧。

Play Framework

诞生时间: 2011
评分: 4/5

使用Play Framework 很容易地创建,构建和发布 web 应用程序,支持 Java & Scala。它使用Akka, 基于一个轻量级的无状态的架构。它应该应用于大规模地低CPU和内存消耗的应用。

链接
官方站点 GITHUB 文档

优点

易于开发

快,但是没有其它的一些框架快

基于 Netty, 支持非阻塞的 I/O. 并行处理远程调用的时候很优秀

社区很大

快速的项目构建和启动

模块化

MVC

REST, JSON/XML, Web Sockets, non-blocking I/O

只需刷新浏览器就可以看到最新的改变

支持Async

有出版的书

缺点

版本2.0 是最有争议的Java框架。 切换至Switch to Scala made some Java developers outraged.

不向后兼容; Play 2.X 重写了

号称轻量级,但有些臃肿

SBT构建工具. 号称 Maven 杀手, 但是从没有优秀到替换它。难以学习和配置

非 servlet

Breaking changes across releases

例子

package controllers
  
import play.api._
import play.api.mvc._
  
class Application extends Controller {
  
  def hello(name: String) = Action {
   Ok( "Hello " + name + "!" )
  }
  
}

注解
抱怨归抱怨,我还是一直喜欢和首选这个框架。不幸的是,我只能给它4颗星。我坚信 基于JAX-RS的框架更适合 RESTful web services.

RestExpress

诞生时间: 2009
评分: 3/5

RestExpress是一个非容器的轻量级的 Netty HTTP栈的包装, 以便更容易地创建 Java RESTful services.
RestExpress 目标是支持最好的 RESTful 实践。

链接
GITHUB

优点

真正的微框架

顶级的性能,快,可靠

XML/JSON

最老的也是最稳定的 RESTful 框架之一

缺点

没有文档

几乎没有支持

很小的社区

例子

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();
   }
}

注解
尽管这个框架超级快,我也不想推荐它。文档缺乏以及没有支持使它成为一个欠佳的框架。看在速度的份上给它3颗星。

Restlet

诞生时间: 2005
评分: 4.5/5

Restlet 帮助Java程序员建立大规模的快速的符合 RESTful 架构模式的web api。
它提供了强大的路由和 filtering 系统。统一的client/server Java API. 满足所有主要的平台 (Java SE/EE, Google AppEngine, OSGi, GWT, Android) 以及提供了无数的扩展以满足程序员的需求。
据我说知,它是第一个 java RESTful web 框架。很多公司都在用它,但是你可能从未听说过它,好像它已经不可见了。

链接
官方站点 GITHUB 文档

优点

强大

企业级的框架

多平台 Java SE, Java EE, Google Web Toolkit, Google AppEngine, Android, OSGi environments

支持JAX-RS (就像 Jersey)

大部分高级 RESTful 支持

模块化

支持其它库

开发一直活跃

智能的url绑定, 全功能的 URI 路由

有相关的书籍

缺点

非常陡峭的学习曲线

关闭的社区,尽管 StackOverflow 上还是开放的

不再流行,更多的是因为 Play Framework 和 Jersey

例子

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" ;
   }
  
}

注解
尽管这个框架还一直流行,但到它的和当前的完成度,我不能给它5颗星。

Restx

诞生时间: 2013
评分: 3.5/5

Restx 是一个轻量级的,模块化的,特性众多的,超快的开源 Java REST 框架。

链接
官方站点 GITHUB 文档

优点

快速,轻量级

容易搭建

真正的微框架

模块化

支持其它库

支持MongoDB

缺点

不友好的令人迷惑的文档。对于这类框架我期望能有好一点的文档

太young

目前还不支持异步Async

例子

@GET ( "/message/{id}" )
   public Message sayHello(String id, // path param
               String who // query param
               ) {
     return new Message().setMessage(String.format(
         "hello %s, it&#39;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" )));
   }

注解
真心来讲我没有在这个框架上花费太多时间。不值得在另一个框架上花费太多精力,我意思是说,Java框架市场已经越来越碎片化了,就像 JavaScript 市场,应该停止这种趋势了。

Spark Framework

诞生时间: 2011
评分: 3.5/5
不要和 Apache 的大数据框架 Spark 弄混, 这里的 Spark 框架是一个轻量级的 Java web 框架,用来进行快速的开发(50% Spark用户使用 Spark 创建 REST APIs)。 它受 Ruby 框架 Sinatra 启发。

它有一个不到1M的最小化的内核, 提供了所有基本的特性, 用来构建 RESTful 或者传统的 web 应用程序。

链接
官方站点 GITHUB 文档

优点

快,轻量级

优秀的快速原型

易于搭建

经常和AngularJS搭配使用

真正的微框架

使用 Jetty

可以用在容器中或者独立运行

缺点

文档可以更好,它不适合初学者

不适合大型项目

社区小

例子

import static spark.Spark.*;
  
public class HelloWorld {
  public static void main(String[] args) {
    get( "/hello" , (req, res) -> "Hello World" );
  }
}

注解
这个框架适合初始开发。主要用作小小项目或者原型。

更多最好的8个Java RESTful框架相关文章请关注PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn