In this brief lesson, we’ll discuss some potential problems with the way we saw many students implementing their server GET /course/ method for MP2. And we’ll challenge you to implement a better solution via a homework problem.
One of the first parts of MP2 was to implement a method as
part of Server.kt
that responded to requests for routes of the form
/course/CS/124
with course details.
Many of you were able to get this to work, which is fantastic!
However, we noticed a few things that many of you could improve. Specifically, here are a few problems that we noticed on many submissions that passed the tests:
courses.json
on each requestcourses.json
on each requestcourses.json
using String
parsing,
which won’t work in general and defeats the purpose of deserialization
entirelyTo be clear, if you passed the tests, then congrats. However, if you made some of these mistakes and showed this code to someone, they might have concerns about your understanding of core language features. So we’re going to return to this problem on a homework problem, where we can help guide you in the right direction.
Note that our concern here is not simply efficiency.
Yes, reloading or reparsing courses.json
on every request is in fact
inefficient.
But, more importantly, it’s also unnecessary.
Ditto with performing a linear search, given that we’ve discussed a data
structure that makes lookups fast and simple.
The homework problem below is set up to mirror the GET /course/
test case from
MP2.
It’s not quite identical: the problem below has you throw an exception rather
than returning an HTTP error code, and it does not support a GET /summary/
route.
You are free to reuse the code that you submitted for MP2. However, if you made some of the mistakes listed above, you will find that you will need to adjust your approach. Here are some hints:
String
read from the file will not be something you can parse
using substring
or other String
methods. You will need to use
deserialization.getCourse
method simple. Once you are done validating the path, it should
only be a few more lines of code to wrap up.We want to support you as you learn to write code that is not only correct, but
idiomatic, elegant, and displays an understanding of how to use language
features appropriate.
We hope that once you complete this problem you will return to your MP2
submission and improve your getCourse
method if it suffered from some of the
flaws listed above.
But that’s up to you.
Let's return to a portion of the 2023–2024 project and examine how to correctly set up a route to deliver course details.
Complete the provided Server
class adding a getCourse
route that accepts a path String
and returns a JSON
object containing details about the requested course.
Valid paths will be in the following format: /course/CS/124
, where the path indicates a request for details
about a course with subject "CS" and number "124".
If the path provided is invalid, throw an IllegalArgumentException
.
If the path provided is valid but the course does not exist, throw an IllegalStateException
.
Otherwise, return the requested course object as a JSON String
.
You will need to load course information out of a courses.json
file, which contains a list of JSON objects in
the following format:
[
{ "subject": "CS", "number": "124", "label": "CS1", "description": "The best course." },
{ "subject": "CS", "number": "107", "label": "DS1", "description": "Another course." },
...
]
The starter code should get you going this the process of loading the JSON into a String
.
You'll need to parse the String
as JSON
to extract the information it contains.
Classes from the Jackson serialization library under com.fasterxml.jackson.core
are available to help with this.
In addition, you can use a Course
class without an import
that is set up with a shape matching the data
in courses.json
.
A few hints to get you started:
courses.json
String
using String
methods like substring
.
The contents are JSON, but the format is intentionally irregular to prohibit this approach.
Use Jackson to properly deserialize the String
.getCourse
should not contain a loop. Solutions that do will timeout.There are several ways to approach this problem, but all valid solutions use serialization (or deserialization), and a data structure that facilitates lookups.
Need more practice? Head over to the practice page.