-
Notifications
You must be signed in to change notification settings - Fork 5
Chapter 8 Lab
David Zemon edited this page Jul 3, 2017
·
1 revision
You've just been shown how to write a simple "Hello World" Spring MVC project. The labs in this Chapter will help you understand the basic components in a Spring MVC project.
- Start be re-mapping the heartbeat controller and its only method so that we can add more controllers and more methods
down the line. Map
HeartbeatControllerto/heartbeatand the existing "Hello, world" service should be mapped toget.
- Implement a service mapped to
reverseinHeartbeatController. The service will have one query parameter of typeStringand return typeMessage. The object returned is simply the input string with the characters in reverse order.- Write the unit Test for the method described above.
- Write the implementation for this method.
- Implement a service mapped to
toUpperinHeartbeatController. The service will have utilize the HTTP request body whose type should be mapped toMessage. The object returned is anotherMessagewith the value of that message being the input string with the characters all converted to upper case.- Write the unit Test for the method described above.
- Write the implementation for this method.
- Create a new Pokemon controller following similar patterns to the heartbeat controller. The controller should have one
endpoint mapped to
/pokemon/getwith theGETverb and a single path variable at the end of the path which is used for the ID of the Pokemon. The endpoint should query the database and return the appropriatePokemonobject for the requested ID.
Add remaining crud operations to the controller.
- Implement the create service. Its path should be
/pokemon/create, it should utilize thePOSTverb and accept a JSON object in its request body with two integer properties:hp, andattack. The service should return a completePokemonobject.- Create a new POJO named
PokemonCreateRequestwhich stores the HP and attack each as typeInteger. Common practice is to put such objects in a new package namedpojoordto(I personally preferpojo). - Write a test case for the create method. It will take a parameter PokemonCreateRequest and will return type Pokemon.
- Implement the create method as described above.
- Create a new POJO named
- Implement the delete method. It should be mapped to
/pokemon/deletewith thePOSTverb and should utilize a single path variable at the end of the path to describe the Pokemon to delete. Its response body should be empty. - Implement the update method. It should be mapped to
/pokemon/updatewith thePOSTverb, it should not have any path or query parameters and it should accept a response body in JSON form with three properties (all integers):id,hp, andattack. It should have an empty response body. Invoking this service should should update the HP and attack of the Pokemon with the provided ID.- Create a new POJO named PokemonUpdateRequest which stores the ID, HP, and attack. Common practice is to put such
objects in a new package named
pojoordto(I personally preferpojo).
- Create a new POJO named PokemonUpdateRequest which stores the ID, HP, and attack. Common practice is to put such
objects in a new package named
We recommend creating new POJOs (as opposed to re-using Pokemon) because request methods such as these have changed frequently in the past, and you might find yourself changing these in future labs too...