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

Create read-only REST endpoint from Entity

    Details

    • Type: Feature Request
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: 1.0.0.Beta2
    • Fix Version/s: 1.0.0.Beta3
    • Component/s: Java EE
    • Labels:
      None

      Description

      /**
       * JAX-RS Example
       *
       * This class produces a RESTful service to read the contents of the members table.
       */
      @Path("/members")
      @RequestScoped
      public class MemberResourceRESTService {
         @Inject
         private EntityManager em;
       
         @GET
         @Produces("text/xml")
         public List<Member> listAllMembers() {
            // Use @SupressWarnings to force IDE to ignore warnings about "genericizing" the results of
            // this query
            @SuppressWarnings("unchecked")
            // We recommend centralizing inline queries such as this one into @NamedQuery annotations on
            // the @Entity class
            // as described in the named query blueprint:
            // https://blueprints.dev.java.net/bpcatalog/ee5/persistence/namedquery.html
            final List<Member> results = em.createQuery("select m from Member m order by m.name").getResultList();
            return results;
         }
       
         @GET
         @Path("/{id:[0-9][0-9]*}")
         @Produces("text/xml")
         public Member lookupMemberById(@PathParam("id") long id) {
            return em.find(Member.class, id);
         }
      }
       
      /**
       * A class extending {@link Application} and annotated with @ApplicationPath is the Java EE 6
       * "no XML" approach to activating JAX-RS.
       *
       * <p>
       * Resources are served relative to the servlet path specified in the {@link ApplicationPath}
       * annotation.
       * </p>
       */
      @ApplicationPath("/rest")
      public class JaxRsActivator extends Application {
         /* class body intentionally left blank */
      }
      

      And need to add JAXB annotation to entity like: @XmlRootElement

        Gliffy Diagrams

          Issue Links

            Activity

            Hide
            lincolnthree Lincoln Baxter III added a comment -

            Done

            Show
            lincolnthree Lincoln Baxter III added a comment - Done
            Hide
            pmuir Pete Muir added a comment -
            • The scaffold doesn't add the @XmlRootElement to the entity
            • The endpoint injects a SMPC which is conversation scoped, but the conversation isn't active in the REST call, better to use @PersistenceContext for now
            Show
            pmuir Pete Muir added a comment - The scaffold doesn't add the @XmlRootElement to the entity The endpoint injects a SMPC which is conversation scoped, but the conversation isn't active in the REST call, better to use @PersistenceContext for now
            Hide
            lincolnthree Lincoln Baxter III added a comment -

            Done.

            Show
            lincolnthree Lincoln Baxter III added a comment - Done.

              People

              • Assignee:
                lincolnthree Lincoln Baxter III
                Reporter:
                lincolnthree Lincoln Baxter III
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Development