2.10. Add a RESTful Web Service


For the TicketMonster application to be able to access the information stored in the database, web services are needed. The procedure below creates a RESTful web service that returns all the events in the database. It generates a POJO (plain old Java object) and adds JAX-RS annotations to create an endpoint.

Procedure 2.10. Add a RESTful Web Service

  1. In the Project Explorer view, expand ticket-monsterJava Resourcessrc/main/java.
  2. Right-click org.jboss.jdf.example.ticketmonster.rest and click NewClass.
  3. In the Name field, type EventService and click Finish. This creates a EventService.java file that is automatically opened in a Java editor.
    In the Name field, type EventService and click Finish.

    Figure 2.18. Completed Name Field in Java Class Wizard

  4. Add the following lines immediately above public class EventService {
    @Path("/events")
    @RequestScoped
    and add the following lines in between the braces of the EventService class
    @Inject
    private EntityManager em;
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Event> getAllEvents() {
    	final List<Event> results =
    		em.createQuery("select e from Event e order by e.name").getResultList();
    	return results;
    }
  5. Save the EventService.java file.
  6. Resolve the errors relating to missing imports by right-clicking anywhere in the EventService class and clicking SourceOrganize Imports.
  7. For each class name that is not unique, select the class to use as follows and click Next:
    • For MediaType select javax.ws.rs.core.MediaType
    • For Event select org.jboss.jdf.example.ticketmonster.model.Event
    • For Produces select javax.ws.rs.Produces
    • For List select java.util.List
    • For Inject select java.inject.Inject
    • For RequestScoped select java.enterprise.context.RequestScoped
    For each class name that is not unique, select the class to use as described here and click Next.

    Figure 2.19. javax.ws.rs.core.MediaType Selected for MediaType Class

  8. When all the classes have been chosen, click Finish. The import statements corresponding to the class names selected in the previous step are added to the EventService.java file.
  9. Save the EventService.java file.
  10. In the Servers view expand jboss-eap-6.x, right-click ticket-monster and click Full Publish to update the deployed version of the application.
  11. Open a new Web Browser and in the address bar of the Web Browser enter http://localhost:8080/ticket-monster/rest/events. This shows the output of the new RESTful endpoint.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.