Learn

Like any web framework that uses HTTP, Spring enables us to respond to GET requests, a method that retrieves the requested information from the server. That information is relayed back to the client and displayed on the browser. Whenever a user navigates to a website or clicks on a URL link, for example, they are initiating a GET request.

Let’s take a look at an example. Imagine we are working with an online system that is keeping track of dog patient intakes for a pet clinic. The website address is http://www.mypetclinic.com/dogs/, where dogs is the resource containing a list of dog objects. In REST environments, the client can access the dogs resource through this GET request:

GET http://www.mypetclinic.com/dogs/

The server, upon receiving this GET request, should return a response that looks something like this:

{ "dogs": [ { "id": 1, "name": "Bella", "breed": "Golden Retriever" }, { "id": 2, "name": "Max", "breed": "Bulldog" }, { "id": 3, "name": "Ruby", "breed": "Poodle" }, ] }

One common way to send a GET request is by typing the URL – the HTTP endpoint – into your local web browser. For example, when a user types http://www.mypetclinic.com/dogs/ into the address bar, the browser

  1. Makes a GET request (GET http://www.mypetclinic.com/dogs/) that retrieves the dogs resource (i.e. the file containing the list of dog intakes) and
  2. Displays the response on the browser page. In doing so, the user confirms that the HTTP endpoint is working – that the GET request is retrieving the requested resources from the Spring server.

Instructions

1.

In the following exercises, we’ll provide you with code to copy-and-paste into the Spring application. For now, try to focus on the overall functionality of the application (i.e. responding to requests) and how to use it (i.e. making requests) rather than syntax. We’ll explain the specifics of the code in a later lesson.

Let’s imagine we’re building a web application that allows users to search for restaurants based on their food allergies. In order to send a GET request that returns a complete list of restaurants in our database, we first need to add an internal GET method to our Spring code.

Copy and paste this GET method at the end of the class, underneath the RestaurantController constructor.

@GetMapping public Iterable<Restaurant> getAllRestaurants() { return restaurantRepository.findAll(); }

Lines of code in Spring apps can be longer than our default code editor. At any point, you can expand the code editor window by either clicking the “maximize” icon in the corner of the code editor or by clicking-and-dragging the right side of the code editor.

2.

To confirm that our GET request is working, type http://localhost:4001/restaurants in the browser’s address bar. A complete list of restaurants should appear on your browser page.

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?