Search code examples
javarestrestful-url

REST API: Using a GET Request to match an ID within a File and display the object corresponding to ID


I really need help with a RESTful API project that I am working on. I have done extensive searches and am at a crossroad. This is very new to me and I am not quite understanding everything, so I do apologize if this is an elementary question.

What I am required to do:

I am required to create a "getVehicle" method that will do the following:

  • getVehicle() will take a given id, and find the vehicle that has the matching id.
  • It will iterate the local file line-by-line, check if the id matches, and if there is a match return the vehicle object.

This is the header:

 @RequestMapping(value = "/getVehicle/{id}", method = RequestMethod.GET)
 public Vehicle getVehicle(@PathVariable("id") int id) throws IOException {

I do know how to do this using purely Java, but this RESTful stuff is throwing me for a loop.

My understanding:

For what I understand is that, when I make my GET request (I use Advance REST Client for this), the ID that I input for {id} will be matched to what is within my text file (called inventory.txt).

I have populated my text file with 6 entries using my POST method, all of which are information about various "vehicles". Therefore, if I make a GET request to url http://localhost808/getVehicle/2, I should get information about the "vehicle" with id 2.

My problem:

However, with what I currently have, my file is not being iterated, so the only "vehicle" that I get is id 0.

What I have so far:

    @RequestMapping(value = "/getVehicle/{id}", method = RequestMethod.GET)
    public Vehicle getVehicle(@PathVariable("id") int id) throws IOException 
    {
        //ObjectMapper provides functionality for reading and writing JSON
        ObjectMapper mapper = new ObjectMapper();

        String inventory = FileUtils.readFileToString(new 
            File("./inventory.txt"),
                CharEncoding.UTF_8);

        //Deserialize JSON to vehicle object
        Vehicle vehicle = mapper.readValue(inventory, Vehicle.class);

        if (vehicle.getId() == id) {
                return vehicle;
        }

        return null;
    }

My Request:

I am at a lost on how to iterate through my text file to find the id that is being requested.

Figuring out this one thing will drastically clear up my confusing and help me reach the completion of my project

ANY hints, tips, resources, anything that will put me on the path of figuring this out will be GREATLY appreciated. Again, this is very nice to me and I am trying to keep my sanity here.

If I need to clarify anything, please let me know!

I give thanks to anyone and everyone who takes the time to help me out here :D!


Solution

  • This question was solved using LineIterator!

    My code is here:

    @RequestMapping(value = "/getVehicle/{id}", method = RequestMethod.GET)
    public Vehicle getVehicle(@PathVariable("id") int id) throws IOException {
    
        //ObjectMapper provides functionality for reading and writing JSON
        ObjectMapper mapper = new ObjectMapper();
    
        //Makes inventory.txt an iterable object
        LineIterator inventory = FileUtils.lineIterator(new File("./inventory.txt"),
                CharEncoding.UTF_8);
    
        //Container for when vehicle is found
        String vehicle = "";
    
        //iterates through inventory.txt until it reaches the end or finds the vehicle via id
        while(inventory.hasNext()) { 
            //Stores each vehicle
            String tempVehicle = inventory.next(); 
            //Creates a vehicle object for JSON
            Vehicle v = mapper.readValue(tempVehicle, Vehicle.class); 
    
            //checks if id of JSON vehicle object matches the id passed through
            if(v.getId() == id) { 
                vehicle = tempVehicle;
            }
        }
        //Returns the found vehicle as a new Objectmapper (still a bit confused about this one)
        return new ObjectMapper().readValue(vehicle, Vehicle.class);
    }