As with GET requests, we can use our local browser to make POST requests, a method that adds new data to a receiving web application.
A common way to append data to an application is by submitting an HTML form. Let’s say we want to add a new German Shepherd named “Charlie” to our list of dog intakes. We simply fill out a form that asks for the name
of the pet (“Charlie”) and breed
(“German Shepherd”). After clicking the “submit” button, the browser sends a POST request with that data to the application. The Spring application then uses this inputted form data to create and add a new dog
object to our existing dogs
resource.
Instructions
To append a new restaurant item to our existing restaurant database, we’re going to send a POST
request by submitting an HTML form. But first (you guessed it!) we’ll need to add a POST
method to our Spring code.
Copy and paste this POST method into the code editor:
@PostMapping @ResponseStatus(HttpStatus.CREATED) public Restaurant addRestaurant(@RequestBody Restaurant restaurant) { validateNewRestaurant(restaurant); return restaurantRepository.save(restaurant); }
A new restaurant item, “Pizza Hut”, is defined by the following attributes and corresponding values:
- Restaurant Name: Pizza Hut
- Address: 123 Main St.
- City: San Diego
- State: CA
- Zip Code: 01567
Add “Pizza Hut” to our restaurant database by
- Typing
http://localhost:4001/addRestaurant.html
in the browser’s address bar - Filling out the HTML form according to the above information
- Clicking “Submit” to make a new POST request