Uploaded image for project: 'Forge'
  1. Forge
  2. FORGE-1212

REST endpoint findById doesn't handle NoResultException

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Major
    • 1.4.2.Final
    • 1.4.1.Final
    • None
    • None

    Description

      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)

      Attachments

        Activity

          People

            rhn-support-ggastald George Gastaldi
            agoncal Antonio Goncalves (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: