Which libraries to develop a Restful API in JAVA?

Asked

Viewed 2,222 times

10

I’m a beginner in java and would like to create a RESTFUL API but I don’t know which library to use or how to use it. Can someone refer me good tutorials or some libraries for study.

I would like to use JSON.

  • I already used the Jetty library (http://www.eclipse.org/jetty/) to make a small REST Webservice. I found it easy for beginners.

  • A basic tutorial for you to create a Restful in Java: http://www.frigglepop.com/wp/? p=45 But I recommend you take a look at Spring as well.

5 answers

10

There are several options to work with REST in Java. There is an implementation of Spring Framework (Spring MVC) and the JEE standard JAX-RS, that has several implementations, among which the ones that I see being more used are Jersey and Resteasy.

For an example of how to use Spring MVC to implement REST see this guide. See also the User Guide from Jersey, it has all the necessary documentation.

One thing you should consider is your production environment. Since Jboss Application Server comes with Resteasy, it makes sense for you to adopt this implementation if you use Jboss, as it will make your life easier. I have already implanted an application with Jersey in a Jboss AS 7.1 and I can say that it was not without difficulties, to the point of having to change configuration files of the modules container. On the other hand, if your application server is Glassfish, it will be easier to go with Jersey which, like Glassfish, is the reference implementation.

  • I use Jetty and I prefer Jersey.

  • @Andrésalvati Jetty is in the list of "technologies that I need to use", but in the corporate world I only see Tomcat, Jboss, Weblogic and Websphere.

6

There are several solutions available in the market, but what I can recommend is learning the pattern. Java EE is a set of standards, and among them is the JAX-RS, which is exactly for REST. Read the specification and see the Resteasy, which is one of the implementations and has several examples.

  • I agree with jpkrohling and only add the play framework if you are looking for productivity. But Resteasy is a good library.

5

The Springmvc api is very good and simple to use. We use it here in the company throughout the project and can deploy with Tomcat/Jetty. Very light and simple. [=

To give you an idea of how simple Springmvc is, the class below would already be exposing a service returning Json:

@Controller
@RequestMapping(value = "/carros")
public class CarrosController {
    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<CarroList> listAll() {
        CarroList carroList = // busca a lista
        return new ResponseEntity<CarroList>(carroList, HttpStatus.OK);
    }
}

With it it is already possible to map the URL of your system, @Requestmapping, the type of the HTTP action, Requestmethod and what will be the return produces.

Here’s a full example where the communication between view and controller is all done with Json.

The code is available in GIT, SVN and the blog itself. [=

http://uaihebert.com/aplicacao-web-completa-angular-twitter-bootstrap-spring-mvc-data-e-security/

4

I would use the Play! Framework. See also the documentation specific to Java.

The route file allows the creation of REST interfaces very easily, for example:

# Home page
GET     /                       controllers.Application.index()

# Tasks          
GET     /tasks                  controllers.Application.tasks()
POST    /tasks                  controllers.Application.newTask()
POST    /tasks/:id/delete       controllers.Application.deleteTask(id: Long)

Using JSON is also very easy. Another example:

import com.fasterxml.jackson.databind.JsonNode;
import play.mvc.BodyParser;
...

@BodyParser.Of(BodyParser.Json.class)
public static Result sayHello() {
  JsonNode json = request().body().asJson();
  String name = json.findPath("name").textValue();
  if(name == null) {
    return badRequest("Missing parameter [name]");
  } else {
    return ok("Hello " + name);
  }
}

Now, talking about why Play! in particular, it allows development in a much more iterative way, where you use the browser directly to debug and see the errors, without the whole ceremony of other alternatives in the Java world. Develop in Play! is much closer to the agility of development in PHP or Ruby on Rails, but without losing the advantages obtained with the use of Java.

2

If you’re looking for a more self-contained solution to create Restful services, check out the Dropwizard. It integrates several well-established tools and Apis (Jersey/JAX-RS, Jetty, etc.) and eliminates the need to set up an application server.

As you’re starting out, it’s a pretty quick way to get something up in the air (compared to having to learn Spring/Java EE subjects and choose application server).

See also:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.