Skip to content

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

  1. Create a new class, MealOrderRequest

  2. Following the JavaBean naming conventions, add a private String property called burger

    • You will have a private instance variable (burger) and two public methods: getBurger and setBurger

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

  1. Create a new class MealOrderResponse

  2. Following the JavaBean naming conventions, add an int property called price

    • You will have a private instance variable (price) and two public methods: a getPrice and a setPrice

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:

  1. Change the HTTP verb in the dropdown from GET to be POST
  2. Put this in the request URL: localhost:8080/api/mealorder
  3. Select the Headers tab underneath and add
    • Key: Content-Type
    • Value: application/json
  4. Select the Body tab and then select the raw radio button
  5. Paste this into the text area: {"burger": "cheese"}
  6. Click on the blue Send button

QuestionBrain

Questions

  1. What happens if you leave out the -H 'Content-Type: application/json' header?
  2. What happens if you don't name the property (burger) correctly, e.g., {"burgerText": "cheese"}?