What libraries to develop a RESTFul API in JAVA?

10

I am a beginner in java and would like to create a RESTFUL API but I do not know which library to use or how to use it. Can anyone point me to good tutorials or some libraries to study.

I would like to use JSON.

    
asked by anonymous 13.12.2013 / 12:34

5 answers

10

There are several options for working with REST in Java. There is an implementation of the Spring Framework (Spring MVC) and the standard JEE JAX-RS , which relies on several implementations, among which the ones I see being the most used are Jersey and RESTEasy .

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

Something you should consider is your production environment. Because JBoss Application Server comes with RESTEasy, it makes sense to adopt this implementation if you use JBoss because it will make your life easier. I've already deployed an application with Jersey on a JBoss AS 7.1 and I can say that it was not without difficulties, to the point of needing to change configuration files of the container modules. On the other hand, if your application server is Glassfish, it will be easier to go with the Jersey that, like Glassfish, is the reference implementation.

    
13.12.2013 / 12:47
6

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

    
13.12.2013 / 12:38
5

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

To get 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);
    }
}

It is already possible to map your system URL, @RequestMapping, HTTP action type, RequestMethod, and what will be the return produces .

Here is a complete example where the communication between view and controller is all done with JSon.

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

link

    
16.12.2013 / 18:48
4

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

The route file allows you to create REST interfaces very easily, such as:

# 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 the Play! in particular, it allows development in a much more iterative way, where you use the browser directly to debug and see the bugs, 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 gained from using Java.

    
13.12.2013 / 19:49
2

If you're looking for a more self-contained solution for creating RESTful services, check out the DropWizard . It integrates several well-established tools and APIs (Jersey / JAX-RS, Jetty, etc.) and eliminates the need to configure an application server.

As you're just getting started, it's a very quick way to put something on the air (compared to having to learn Spring / Java EE issues and choosing application server).

See also:

13.12.2013 / 13:52