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

REST endpoint findById doesn't handle NoResultException

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: 1.4.1.Final
    • Fix Version/s: 1.4.2.Final
    • Component/s: None
    • Labels:
      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)

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                gastaldi George Gastaldi
                Reporter:
                agoncal Antonio Goncalves
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: