Lab 9 - JSON Request and Response
JSON Request and Response¶
In this lab you'll use the magic of automatic conversion of JSON to JavaBean objects and vice-versa to allow your API to accept and return JSON instead of a plain String
.
JavaBean
A JavaBean is a Java class that has named properties, defined by getters and setters that follow
a naming convention. The following example is a class that has a String
property named Description
:
public class Product {
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
You can also have numeric properties, which follow the same naming pattern. For example:
public class Product {
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
Create MealOrderRequest Transfer Object¶
This class represents the incoming request for a meal order. It should have one property:
- The text for the burger toppings, called burger
Steps to Create¶
-
Create a new class,
MealOrderRequest
-
Following the JavaBean naming conventions, add a private
String
property calledburger
- You will have a private instance variable (
burger
) and two public methods:getBurger
andsetBurger
- You will have a private instance variable (
Create MealOrderResponse Transfer Object¶
This is the response that the server will send back to the client. It will have a single property:
- The price of the meal (as an
int
), called price
Steps to Create¶
-
Create a new class
MealOrderResponse
-
Following the JavaBean naming conventions, add an
int
property calledprice
- You will have a private instance variable (
price
) and two public methods: agetPrice
and asetPrice
- You will have a private instance variable (
Accept a POST¶
In the MealOrderApiController
class, delete the existing method and replace it with this one:
@PostMapping(value = "/api/mealorder", consumes = MediaType.APPLICATION_JSON_VALUE)
public MealOrderResponse mealOrder(@RequestBody MealOrderRequest mealOrderRequest) {
// extract the burger text from mealOrderRequest
mealOrderRequest...??
// create a meal order using the MealBuilder
// Take a look at MealBuilderTest for examples
int price = ...?? // where do we get the price from?
//-- Return
MealOrderResponse mealOrderResponse = new MealOrderResponse();
// set the price to the price from the mealOrder
mealOrderResponse.setPrice(...??); // where do you get the price from?
return mealOrderResponse;
}
Import the Correct MediaType
The MediaType.APPLICATION_JSON_VALUE
needs an additional import
statement:
import org.springframework.http.MediaType;
Try it With curl¶
Start the application and using curl
, do a POST to /api/mealorder
with the burger
property.
For example:
curl -v -d '{"burger": "cheese"}' -H 'Content-Type: application/json' localhost:8080/api/mealorder
Try it With Postman¶
Launch Postman and do the following:
- Change the HTTP verb in the dropdown from GET to be POST
- Put this in the request URL:
localhost:8080/api/mealorder
- Select the Headers tab underneath and add
- Key:
Content-Type
- Value:
application/json
- Key:
- Select the Body tab and then select the raw radio button
- Paste this into the text area:
{"burger": "cheese"}
- Click on the blue Send button
Questions¶
- What happens if you leave out the
-H 'Content-Type: application/json'
header? - What happens if you don't name the property (
burger
) correctly, e.g.,{"burgerText": "cheese"}
?