DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Testing REST Controller Methods With JUnit 5 [Video]
  • Update User Details in API Test Client Using REST Assured [Video]

Trending

  • What Technical Skills Can You Expect To Gain From a DevOps Course Syllabus?
  • CI/CD Tools and Technologies: Unlock the Power of DevOps
  • Microservices With Apache Camel and Quarkus
  • The State of Data Streaming for Telco
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. How to Combine REST Services with EJB 3.1

How to Combine REST Services with EJB 3.1

Want to combine REST services using EJB 3.1? Learn how in this great tutorial.

Milan Kuchtiak user avatar by
Milan Kuchtiak
·
Dec. 01, 09 · Tutorial
Like (1)
Save
Tweet
Share
136.66K Views

Join the DZone community and get the full member experience.

Join For Free

REST services (JAX-RS) technology became a part of the Java EE 6 platform, which is great news for REST developers.

In a very simple web application, I'll demonstrate how REST services can be combined with other Java EE technologies, specifically EJB 3.1.

  1. First of all, we need to specify a REST resources path. Previously, in Java EE 5, the only way to do this was to register a specific servlet adaptor in web.xml:
         <servlet>        <servlet-name>ServletAdaptor</servlet-name>        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>ServletAdaptor</servlet-name>        <url-pattern>/resources/*</url-pattern>    </servlet-mapping>
    JAX-RS API (from version 1.1.4) introduced  a specific annotation (@javax.ws.rs.ApplicationPath), that provides an alternative to web.xml configuration:
     package org.netbeans.rest.application.config;/** * This class is generated by the Netbeans IDE, * and registers all REST root resources created in the project. * Please, DO NOT EDIT this class ! */@javax.ws.rs.ApplicationPath("resources")public class ApplicationConfig extends javax.ws.rs.core.Application {}
    Note: The ApplicationConfig class extends javax.ws.rs.core.Application. There are 2 methods that may be implemented from the super class:
         public Set<Class<?>> getClasses();     public Set<Object> getSingletons();
    These methods specify a set of root resources and providers accessible from this application path. This way users may specify several groups of REST resources (not necessarily disjunctive), in a single web application. NetBeans IDE 6.8 provides a default implementation of javax.ws.rs.core.Application containing all REST root resources and providers specified in a web application. Users can specify a URL string (e.g. "resources") that will be used to access these resources. Users can also implement a custom subclass(es) of javax.ws.rs.core.Application.
  2. Secondly, we create a Singleton EJB class that will be used to persist the state of REST resource (created in next stem). This is the class:
    package name;import javax.annotation.PostConstruct;import javax.ejb.Singleton;@Singletonpublic class NameBean {       private String name;|       @PostConstruct    private void init() {        name="Robin Hood";    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}
    Note: The singleton EJB is a new feature in Java EE 6. The container is guaranteed to maintain a single shared instance of an EJB 3.1 Singleton. This means that Singletons can cache state across the application tier. The @PostContruct annotation is used to annotate a method performing any initialization stuff, and it's called by container just after a single instance is created.
  3. The last step is to create a simple REST root resource, using the singleton NameBean to persist its state:
    package name;import javax.ejb.*;import javax.ws.rs.*;@Path("name")@Statelesspublic class NameService {    @EJB    private NameBean nameBean;    @GET    @Produces("text/html")    public String getHtml() {        return "<h2>Hello "+nameBean.getName()+"</h2>";    }    @PUT    @Consumes("text/plain")    public void put(String content) {        nameBean.setName(content);    }}
    Note: Dependency Injection is used to inject the NameBean into the resource. The EJB 3.0 @Stateless annotation is a convenient way to enable dependency injection. If this annotation is missing, the nameBean field won't be initialized by the container. The other annotations come from the JAX-RS (JSR311) specification.

NetBeans IDE provides a RESTful, JavaScript based, tester capability that can be used to test HTTP GET, POST, PUT or DELETE methods. This is an example of invoking HTTP PUT on REST resource located at "http://localhost:8080/RestApplication/resources/name":

Netbeans REST Tester

Finally, the HTTP GET method can also be tested easily by typing resource URI in the browser:

HTP GET test

REST Web Protocols

Opinions expressed by DZone contributors are their own.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Testing REST Controller Methods With JUnit 5 [Video]
  • Update User Details in API Test Client Using REST Assured [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: