Returning JSON

Let's try to return our first JSON object with JAX-RS. It is so easy that you don't need to handle any special stuff yourself, just return your object from a method declared to return JSON's media type using the @Produces annotation, nothing more!

In the following example, we are going to simulate an in-memory database of movies using a HashMap. We are going to create a map of movies, where the key is the movie's id (primary key), and the value is an object representing a movie in some database. We will support two operations in this example:

  • Retrieving a movie by id
  • Listing all movies found in our database

Before we go on with this example, let's discuss how we should design our RESTful services. The dominant convention ...

Get Developing Middleware in Java EE 8 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.