title: REST API: Using a GET Request to match an ID within a File and display the object corresponding to ID
body: <p>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. </p>
  
  <p><strong>What I am required to do:</strong></p>
  
  <p>I am required to create a "getVehicle" method that will do the following:</p>
  
  <ul>
  <li>getVehicle() will take a given id, and find the vehicle that has the
  matching id. </li>
  <li>It will iterate the local file line-by-line, check if
  the id matches, and if there is a match return the vehicle object.</li>
  </ul>
  
  <p><strong>This is the header:</strong></p>
  
  <pre><code> @RequestMapping(value = "/getVehicle/{id}", method = RequestMethod.GET)
   public Vehicle getVehicle(@PathVariable("id") int id) throws IOException {
  </code></pre>
  
  <p>I do know how to do this using purely Java, but this RESTful stuff is throwing me for a loop.</p>
  
  <p><strong>My understanding:</strong> </p>
  
  <p>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).</p>
  
  <p>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 <a href="http://localhost808/getVehicle/2" rel="nofollow noreferrer">http://localhost808/getVehicle/2</a>, I should get information about the "vehicle" with id 2.</p>
  
  <p><strong>My problem:</strong></p>
  
  <p>However, with what I currently have, my file is not being iterated, so the only "vehicle" that I get is id 0.</p>
  
  <p><strong>What I have so far:</strong></p>
  
  <pre><code>    @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;
      }
  </code></pre>
  
  <p><strong>My Request:</strong></p>
  
  <p>I am at a lost on how to iterate through my text file to find the id that is being requested.</p>
  
  <p>Figuring out this one thing will drastically clear up my confusing and help me reach the completion of my project</p>
  
  <p>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.</p>
  
  <p><strong>If I need to clarify anything, please let me know!</strong></p>
  
  <p>I give thanks to anyone and everyone who takes the time to help me out here :D!</p>