Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4536

REST API: JAX-RS vs Spring

$
0
0

1. Overview

In this tutorial, we'll see the difference between JAX-RS and Spring MVC for REST API development.

2. Jakarta RESTful Web Services

To become part of the JAVA EE world, a feature must have a specification, a compatible implementation, and a TCK. Accordingly, JAX-RS is a set of specifications for building REST services. Its best-known reference implementations are RESTEasy and Jersey.

Now, let's get a little familiar with Jersey by implementing a simple controller:

@Path("/hello")
public class HelloController {
    @GET
    @Path("/{name}")
    @Produces(MediaType.TEXT_PLAIN)
    public Response hello(@PathParam("name") String name) {
        return Response.ok("Hello, " + name).build();
    }
}

Above, the endpoint returns a simple “text/plain” response as the annotation @Produces states. Particularly, we are exposing a hello HTTP resource that accepts a parameter called name with two @Path annotations. We also need to specify that it is a GET request, using the annotation @GET.

3. REST With Spring MVC

Spring MVC is a module of Spring Framework for creating web applications. It adds REST capability to Spring Framework.

Let's make the equivalent GET request example as above, using Spring MVC:

@RestController
@RequestMapping("/hello")
public class HelloController {
    @GetMapping(value = "/{name}", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<?> hello(@PathVariable String name) {
        return new ResponseEntity<>("Hello, " + name, HttpStatus.OK);
    }
}

Looking at the code, @RequestMapping states that we're dealing with a hello HTTP resource. In particular, through the @GetMapping annotation, we're specifying that it is a GET request. It accepts a parameter called name and returns a “text/plain” response.

4. Differences

JAX-RS hinges on providing a set of Java Annotations and applying them to plain Java objects. Indeed, those annotations help us to abstract the low-level details of the client-server communication. To simplify their implementations, it offers annotations to handle HTTP requests and responses and bind them in the code. JAX-RS is only a specification and it needs a compatible implementation to be used.

On the other hand, Spring MVC is a complete framework with REST capabilities. Like JAX-RS, it also provides us with useful annotations to abstract from low-level details. Its main advantage is being a part of the Spring Framework ecosystem. Thus, it allows us to use dependency injection like any other Spring module. Furthermore, it integrates easily with other components like Spring AOP, Spring Data REST, and Spring Security.

5. Conclusion

In this quick article, we looked at the main differences between JAX-RS and Spring MVC.

As usual, the source code for this article is available over on GitHub.

The post REST API: JAX-RS vs Spring first appeared on Baeldung.
       

Viewing all articles
Browse latest Browse all 4536

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>