Learn

The term REST stands for REpresentational State Transfer, and it describes the architectural technique used to facilitate communication between different systems via the web. Stateless requests are sent from a client to either retrieve or make changes to a resource stored on a server. The server provides a response based on the information received in the request. Check out this article to learn more about REST architecture.

In order to provide a response to a request, the server maps the request to an endpoint. In Spring, a class is marked as a controller and each of its methods is associated with an endpoint. The method is called to formulate a response to each request. This is facilitated by two important annotations:

  • The @RestController annotation is used to declare a class as a controller that can provide application-specific types of data as part of an HTTP response
  • The @RequestMapping annotation is used to wire request types and endpoints to specific class methods.

The @RestController annotation should be added to each class that will handle responses to HTTP requests. The @RestController combines the functionality of two separate annotations, @Controller and @ResponseBody.

  • @Controller is used to make a class identifiable to Spring as a component of the web application.
  • @ResponseBody tells Spring to convert the return values of a controller’s methods to JSON and bind that JSON to the HTTP response body. Since almost every major programming language offers some type of JSON (JavaScript Object Notation) support, we can easily take an object created in Spring and translate it to JSON which can be easily parsed in the HTML and displayed on the page.

@RequestMapping accepts several arguments. The first, and perhaps most important, is the path. The path argument is used to determine where requests are mapped. For example, if a user makes a request to get a “book” resource, the server will send the request to a method with the annotation @RequestMapping(path = "/book") and that method would provide the response.

The @RequestMapping annotation also accepts several other arguments including method, params, and consumes. Let’s take a look at them.

  • method: allows the developer to specify which HTTP method should be used when accessing the controller method. The most common are RequestMethod.GET, ...POST, ...PUT, and ...DELETE. Since this is an optional argument, if we do not specify an HTTP method it will be defaulted to GET.

  • params: filters requests based on given parameters.

  • consumes: used to specify which media type can be consumed; some common media types are "text/plain", "application/JSON", etc.

@Controller public class VirtualLibrary{ @RequestMapping("/books/all", RequestMethod.GET) public Book getAllBooks() { return getBook(); } }

Instructions

1.

Ingrid is the newest superhero in Powersville. Her superpower is invisibility and she wants to make new friends and start helping the citizens of Powersville right away. She’s creating a Spring application to help her tell others about her superpower. Let’s help Ingrid get her application up and running.

Note the methods included in our class. Use curl (curl localhost:4001/superHeroes) to make a request for the superHeroes resource. Did the application respond to your HTTP request the way you were expecting?

If, at any point, you see an error like “Failed to connect to localhost port 4001: Connection refused”, your Spring server may be malfunctioning. Fix all errors in your code, and run your code before executing the command pkill ein in the terminal to reset the server. Your browser will disconnect and reconnect to Codecademy.

2.

The application could not find the resource to provide the correct response to your HTTP request in the previous checkpoint because the server could not find a class that handles HTTP requests.

We will need to declare a controller to do this. Before we can do that, we need to add an import statement that will allow us to use Spring annotations.

Add this import statement above your class declaration:

import org.springframework.web.bind.annotation.RestController;
3.

Now that we’re ready to use annotations, declare the class as a REST controller.

4.

A response for /superHeroes was not generated because there is no method mapped to that endpoint.

Use @RequestMapping to register the getSuperHeroes method as an endpoint for GET requests to /superHeroes.

Remember that you don’t need to specify a request method here: it’s GET by default!

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?