I am developing a simple RESTful service for tournaments and schedules. When a tournament is created through a POST request containing a JSON body, the tournament is inserted in a BiMap
, declared as follows in a DAO implementation:
private BiMap<String, Tournament> tournaments = Maps.synchronizedBiMap(HashBiMap.create());
When a tournament is created, its associated string id is returned so the user can have future reference of that tournament. He/she can get information back from the new tournament performing the following request:
GET http://localhost:8080/eventscheduler/c15268ce-474a-49bd-a623-b0b865386f39
But what if no tournament with such id is found? So far, I am returning a 204 response. Well, Jersey is doing it for me when returning null
from one of its methods. This is the method that corresponds to the route above:
@Path("/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Tournament getTournament(@PathParam("id") String id) {
Optional<Tournament> optTournament = tournamentDao.getTournament(id);
if (optTournament.isPresent())
return optTournament.get();
return null;
}
My question is: is it OK to return a 204: No Content
response, or should it be a 404
response instead, since the resource was not found?
If I should change it to a 404, obvious question: I should change the method signature right? Since now a tournament (of type Tournament
) might not be returned, the method should look different. Should I use the Response
type as the return type instead?