MyArchiBook

Archive for the ‘Java EE 7’ Category

Unsatisfied Dependency Exception : CDI 1.1 + Bean Validation + GlassFish 4.0 -> requires jersey-gf-cdi.jar upgrade

with one comment

Recently,  I faced Unsatisfied Dependency Exception, when trying to integrate BeanValidation with CDI 1.1 . Independently (without integration) everything worked fine. Looks like, the present Glassfish version 4.0 doesn’t support CDI 1.1 + Bean Validation together.

To be still clearer, Jersey (reference implementation for JAX-RS)  jar related to CDI  i.e., jersey-gf-cdi.jar in GF-4.0 version, is not supporting this integration .

I made the below changes, for Bean Validation integration with CDI 1.1 in Glassfish 4.0 to work.

Applicaiton Server used : Glassfish 4.0

Technology : JEE-7, CDI 1.1

Exception Trace :

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=Foo,parent=Foo,qualifiers={@foo()}),position=-1,optional=false,self=false,unqualified=null,350112977)

at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:771)
at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:790)
at org.glassfish.jersey.gf.cdi.CdiComponentProvider$1.inject(CdiComponentProvider.java:316)
at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:158)
at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69)

………………………….

Solution :

  • Download jersey-gf-cdi-2.0.jar . Download specifically 2.0 version for glassfish4.0. If other higher versions 2.10 are used, incompatibilty with other jars (firstly with jersey-server.jar) will arise.

http://repo1.maven.org/maven2/org/glassfish/jersey/containers/glassfish/jersey-gf-cdi/2.0/jersey-gf-cdi-2.0.jar

  • Rename the jar to jersey-gf-cdi.jar
  • Replace it in glassfish4.0/glassfish/modules/
  • Delete osgi-cache folder -> Path : glassfish4.0/glassfish/domains/domain1/osgi-cache
  • Refresh and restart glassfish. New osgi-cache will be created in domain 1 with the latest jersey-gf-cdi.jar. Problem will be solved 🙂

This issue will be resolved in Glassfish-4.0.1 version (not released yet). We’ll have to do the above till GF-4.0.1 is released.

Have a great day 🙂

Reference :
https://java.net/jira/browse/GLASSFISH-20597

Advertisements

EJB class able to render REST WebService via Annotation

with 3 comments

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
URL

The result is obtained as below.

Output

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 🙂