@RequestMapping(value = "/getBowler/{id}", method = RequestMethod.GET) public ResponseEntity<String> getBowlerById(@PathVariable int id) { List<Bowler> listOfBowlers = new ArrayList<Bowler>(BowlingClub.bowlers); for (Bowler b : listOfBowlers) { if (b.getID() == id) { Gson gson = new Gson(); String response = gson.toJson(b); return new ResponseEntity<>(response, HttpStatus.OK); } } CustomError e = new CustomError("Invalid Bowler ID."); Gson gson = new Gson(); String response = gson.toJson(e); return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); }