-
Bug
-
Resolution: Done
-
Major
-
1.4.1.Final
-
None
-
None
When a REST endpoint is generated the findById method looks like this :
@GET
@Path("/{id:[0-9][0-9]*}")
@Produces("application/xml")
public Response findById(@PathParam("id") Long id) {
TypedQuery<Book> findByIdQuery = em.createQuery("SELECT DISTINCT b FROM Book b WHERE b.id = :entityId ORDER BY b.id", Book.class);
findByIdQuery.setParameter("entityId", id);
Book entity = findByIdQuery.getSingleResult();
if (entity == null) {
return Response.status(Status.NOT_FOUND).build();
}
return Response.ok(entity).build();
}
The problem is that when the entity doesn't exist, the findByIdQuery.getSingleResult() doesn't return null but throws a NoResultException. So the if (entity == null) is never reached.
The generated code should catch the NoResultException and return the return Response.status(Status.NOT_FOUND)