5. Spring MVC And Rest Endpoint - pnguye35/pal-tracker GitHub Wiki

Purpose In this lab, you will build a Time Entry service which will expose a RESTful API for time entries. The lab will introduce the fundamentals of Spring MVC for building web services.

Learning Outcomes After completing the lab, you will be able to:

Employ the repository design pattern to build an in-memory data store Develop RESTful JSON APIs using Spring MVC controllers Demonstrate the ability to manually test a JSON API Describe when to use the @Bean annotation Get started Before starting the lab, pull in some failing tests using Git:

git cherry-pick mvc-start Our goal is to get our test suite passing by the end of the lab.

Time Entries CRUD You will build a service that can do CRUD (Create, Read, Update, Delete) operations on time entries by first defining its API. Begin by implementing create, view, update and delete actions on time entries and persisting them with a repository. At first, the repository will be backed by a HashMap. It will be replaced by a database in a later lab.

Data layer Create a TimeEntry class with the following fields:

private long id; private long projectId; private long userId; private LocalDate date; private int hours; Your code will not compile at this point because we cherry-picked additional tests for classes which are not implemented. Create empty classes and stub methods so that your code compiles. Do not move on until the following command passes (indicating that your code compiles).

./gradlew clean compileTestJava Implement the InMemoryTimeEntryRepository class which implements the TimeEntryRepository interface, using the InMemoryTimeEntryRepositoryTest as a guide.

Run the test for your new repository and make sure that it passes before moving on.

Declare a @Bean method which returns your implementation of the TimeEntryRepository in the PalTrackerApplication class.

REST controller Open the TimeEntryController class.

Controllers in Spring MVC are Java objects with an annotation that signals to Spring that they are controllers. You are building a time entry service so this controller will expose a RESTful API for time entries.

For convenience, use the @RestController annotation. This is a regular Spring MVC Controller but it also includes the @ResponseBody annotation which automatically serializes objects into JSON when they are returned from a handler method.

Controller handler methods are annotated with @RequestMapping, or the short hand versions @GetMapping, @PostMapping, etc. The annotation takes the URL that the handler will respond to as an argument.

All of your controller methods will return a ResponseEntity type. This allows you to return objects (serialized into JSON) and to have control of the HTTP status codes.

Use the TimeEntryControllerTest and then the TimeEntryApiTest to guide you to build the TimeEntryController implementation.

Run the full test suite with

./gradlew clean build and once they are all green you are ready to move on.

Exercise endpoints Start your application locally and use the curl commands below to verify that your application is behaving as expected.

Get all time entries

curl -i localhost:8080/time-entries Create a time entry

curl -i -XPOST -H"Content-Type: application/json" localhost:8080/time-entries -d"{"projectId": 1, "userId": 2, "date": "2019-01-01", "hours": 8}" Get a time entry by ID

curl -i localhost:8080/time-entries/${TIME_ENTRY_ID} Update a time entry by ID

curl -i -XPUT -H"Content-Type: application/json" localhost:8080/time-entries/${TIME_ENTRY_ID} -d"{"projectId": 88, "userId": 99, "date": "2019-01-01", "hours": 8}" Delete a time entry by ID

curl -i -XDELETE -H"Content-Type: application/json" localhost:8080/time-entries/${TIME_ENTRY_ID} When you are satisfied that your application is behaving correctly locally you are ready to move on.

Deploy Now that everything is working locally, push your code to GitHub and let the pipeline deploy to the review environment.

Submit this assignment Submit the assignment using the cloudNativeDeveloperRest gradle task. It requires you to provide the URL of your application. For example:

cd ~/workspace/assignment-submission ./gradlew cloudNativeDeveloperRest -PserverUrl=https://${APP_URL} Learning Outcomes Now that you have completed the lab, you should be able to:

Employ the repository design pattern to build an in-memory data store Develop RESTful JSON APIs using Spring MVC controllers Demonstrate the ability to manually test a JSON API Describe when to use the @Bean annotation Extra If you have additional time, try implementing a new API version alongside the current time entry controller. Take a look at the App Continuum documentation for ideas on how to implement api versioning.