Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Java RESTful framework

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to use the Java RESTful framework". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Dropwizard

Time of birth: 2011

Score: 4.5 pound 5

Dropwizard provides a stable and mature Java library that is packaged into a simple lightweight package.

Dropwizard is between the framework and the library. It provides everything you need to develop a web application. Due to the built-in modularity, an application can remain small and lean, reduce development and maintenance time, and reduce burden.

Dropwizard uses existing Jetty HTTP libraries to embed into your project without the need for external server. All Dropwizard projects have a main method to manage the built-in HTTP server.

Link

Official site GITHUB documentation

Advantages

Rapid project build and launch

Modularization

Incredibly fast (at least based on the results 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 using Metrics support

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

The community is strong.

Shortcoming

Dropwizard documents are the main source of knowledge, but not excellent. You may need to search and discover documentation for third-party class libraries.

Errors are treated as plain text for some reason, which may be problematic if you want the response result to be always JSON

Be sure to use * * Dropwizard, some older versions use obsolete third-party libraries. And the early Dropwizzard is also very difficult to upgrade.

Examples

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 {

Public static void main (String [] args) throws Exception {

New HelloWorldApplication () run (args)

}

@ Override

Public String getName () {

Return "hello-world"

}

@ Override

Public void initialize (Bootstrap bootstrap) {

/ / nothing to do yet

}

@ Override

Public void run (HelloWorldConfiguration configuration

Environment environment) {

/ / nothing to do yet

}

}

Notes

Individuals do not want to recommend this framework for large projects. But if you want to try, you won't be disappointed. The main reason is that this framework is assembled into an easy-to-use framework using modern Java web components.

Unfortunately, this also brings its problems. Mixing these libraries can lead to unforeseen problems. That's why I lost 0.5 stars to it instead of giving it a full score of 5 stars.

Jersey

Birth time: 2012 (Jersey 2.x)

Score: 5pm 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 reference implementation and provides more features and tools to further simplify RESTful service and client development. Although relatively young, it is already a production-level RESTful service and client framework.

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.

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

Support for asynchronous links

Don't like servlet container? You can use Jersey without them.

WADL, XML/JSON support

Included in Glassfish

Shortcoming

Jersey 2.0 + uses a somewhat complex dependency injection implementation

Maybe it's not a bad thing. Jersey 1.x uses the older JAX-RS implementation

A large number of third-party libraries only support Jersey 1.x and are not available on Jersey 2.x

Examples

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

}

}

Notes

Jersey is my choice, 5 stars.

Ninja Web Framework

Time of birth: 2012

Score: 3.5amp 5

Ninja Web Framework is a full-stack java web framework. Stable, fast, reliable, product grade.

It provides everything to develop, test, release, and maintain RESTful web applications (Servlets, Guice, JPA, Flyway migrations, Maven, etc.).

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

Come on!

Rapid project build and launch

Modularization

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

Again, like DropWizzard, the documentation is there but 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 information you need.

Not very famous, small community. Rumor has it that the framework was created by Play 2.x users who switched to Scala.

Examples

Package controllers

Public class ApplicationController {

Public Result index () {

Person person = new Person ()

Person.name = "John Johnson"

Return Results.json () render (person)

}

}

Notes

It looks good, but I'll put it aside until it's mature.

Play Framework

Time of birth: 2011

Score: 4thumb 5

Using Play Framework, it is easy to create, build, and distribute web applications that support Java & Scala. It uses Akka and is based on a lightweight stateless architecture. It should be used in 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 frameworks

Based on Netty, it supports the non-blocking iAccord O. Excellent for parallel processing of remote calls

The community is very large.

Rapid project build and launch

Modularization

MVC

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

You only need to refresh the browser to see the changes.

Support for Async

There are published books.

Shortcoming

Version 2.0 is the most controversial Java framework. Switch to Switch to Scala made some Java developers outraged.

Not backward compatible; Play 2.x is rewritten

It is known as lightweight, but it is a little bloated.

SBT build tool. Known as the Maven killer, but never good enough to replace it. Difficult to learn and configure

Non-servlet

Breaking changes across releases

Examples

Package controllers

Import play.api._

Import play.api.mvc._

Class Application extends Controller {

Def hello (name: String) = Action {

Ok ("Hello" + name + "!")

}

}

Notes

Complaining but complaining, I still like the framework of * *. Unfortunately, I can only give it four stars. I firmly believe that JAX-RS-based frameworks are more suitable for RESTful web services.

RestExpress

Time of birth: 2009

Score: 3tick 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 RESTful practices.

Link

GITHUB

Advantages

A real micro-framework.

The performance of * is fast and reliable.

XML/JSON

One of the oldest and most stable RESTful frameworks

Shortcoming

No documentation

There is little support.

A very small community

Examples

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

}

}

Notes

Although this framework is super fast, I don't want to recommend it. Lack of documentation and lack of support make it a poor framework. Give it three stars for speed's sake.

Restlet

Time of birth: 2005

Score: 4.5 pound 5

Restlet helps Java programmers build large-scale and fast web api that conforms to the RESTful architecture pattern.

It provides powerful routing and filtering systems. Unified client/server Java API. Meet all major platforms (Java SE/EE, Google AppEngine, OSGi, GWT, Android) and provide numerous extensions to meet the needs of programmers.

As far as I know, it is a java RESTful web framework. Many companies use it, but you may never have heard of it, as if it is no longer visible.

Link

Official site GITHUB documentation

Advantages

Powerful

Enterprise-level framework

Multi-platform Java SE, Java EE, Google Web Toolkit, Google AppEngine, Android, OSGi environments

Support for JAX-RS (like Jersey)

Most advanced RESTful support

Modularization

Support for other libraries

Development has been active all the time

Intelligent url binding, full-featured URI routing

There are related books.

Shortcoming

A very steep learning curve

Closed communities, even though they are still open on StackOverflow

It is no longer popular, it is more because of Play Framework and Jersey

Examples

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"

}

}

Notes

Although this framework has always been popular, given its age and current completion, I can't give it five stars.

Restx

Time of birth: 2013

Score: 3.5amp 5

Restx is a lightweight, modular, feature-rich, ultra-fast open source Java REST framework.

Link

Official site GITHUB documentation

Advantages

Fast, lightweight

Easy to build

A real micro-framework.

Modularization

Support for other libraries

Support for MongoDB

Shortcoming

An unfriendly and confusing document. I expect better documentation for this kind of framework.

Too young

Currently, asynchronous Async is not supported.

Examples

@ 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"))

}

Notes

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, and it's time to stop.

Spark Framework

Time of birth: 2011

Score: 3.5amp 5

Don't be confused with Spark, Apache's big data framework, which 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 for building RESTful or traditional web applications.

Link

Official site GITHUB documentation

Advantages

Quick, lightweight.

Excellent rapid prototyping

Easy to build

Often used in conjunction with AngularJS

A real micro-framework.

Use Jetty

Can be used in a container or run independently

Shortcoming

The document can be better, it is not suitable for beginners

Not suitable for large-scale projects

Small community

Examples

Import static spark.Spark.*

Public class HelloWorld {

Public static void main (String [] args) {

Get ("/ hello", (req, res)-> "Hello World")

}

}

Notes

This framework is suitable for initial development. Mainly used as a small project or prototype.

This is the end of the content of "how to use the Java RESTful Framework". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report