Posts Tagged ‘Resource’
EJB class able to render REST WebService via Annotation
Advancements in Java, especially the advent of JEE6 and JEE7 has eased the way we code.
Deployment descriptors for Servlet classes, Entity classes, Web Services are being replaced by annotations, thus advancing towards the concept of Convention over Configuration.
Below is a simple example on how to make a Session Bean class provide a RESTful WebService using annotations.
Here I have used JEE 7 and GlassFish 4 Application Server.
Note: As of now, JEE7 is compatible only with GlassFish 4.
Here I have considered WebsterPublications example which has,
- DictionaryEntity – Entity class
- DictionaryResource – EJB+REST Service Provider Class
STEP 1: Create DictionaryEntity.java
@Entity @Table(name = "WP_DICTIONARY") public class DictionaryEntity { @Id @GeneratedValue private Long id; @NotNull(message = "Enter a word") private String word; @NotNull(message = "Enter the word's meaning") private String meaning; //Getters , Setters }
Attributes can be annotated with constraints like @NotNull, @Pattern and can be made to through message during runtime, when violations occur.
STEP 2: Create DictionaryResource.java
@Path(value = "dictionary") @Stateless public class DictionaryResource { @PersistenceContext(unitName = "webster") private EntityManager em; @GET @Produces(MediaType.APPLICATION_JSON) @Path(value = "wordlist") public List<DictionaryEntity> getWordList(){ Query query=em.createQuery("select d from DictionaryEntity d"); List<DictionaryEntity> wordList=query.getResultList(); return wordList; } }
Here we can notice that this class apart from acting as a Stateless Bean also provides a RESTful WebService.
i.e., stateless session bean class providing the output object via REST WebService (Here JSON object got as Output).
Mapping the persistence unit and connection pooling in glassfish server is done (default Derby DB used).
STEP 3: Create WebsterRESTConfig.java
Since DictionaryResource acts like a Servlet by providing WebService, it’s essential to provide its entry in web.xml.
But this can also be done through annotation even.
WebsterRESTConfig class is created that extends Application and adds DictionaryResource as its resource class.Like DictionaryResource various other resource class can also be added.
@javax.ws.rs.ApplicationPath("webresources") public class WebsterRESTConfig extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new java.util.HashSet<>(); addRestResourceClasses(resources); return resources; } private void addRestResourceClasses(Set<Class<?>> resources) { resources.add(aish.vaishno.websterpublications.resource.DictionaryResource.class); } }
Here @ApplicationPath is equivalent to the servlet-mapping URL-Pattern that we would specify in web.xml.
Also note, while instantiating Set<Class<?>> resources = new java.util.HashSet<>(); I haven’t used generics in HashSet<> but have just used a plane Diamond notation. This is one of the light-weight changes in Java SE 7.
Now when we give the path as:
http://localhost:8080/WebsterPublications/webresources/dictionary/wordlist
The result is obtained as below.
In my this post I wanted to highlight on the concept of Convention Over Configuration which is taking a great leap as the advancements in JEE versions are happening.Without even taking the pain to write the conventional web.xml deployment descriptor, we are now that able to configure those settings by using simple annotations at the right place.
You can find the above source code in my GitHub repository.
I wish you all a BLASTFUL 2014 🙂