in jersey 2.x am getting the error like java.util.concurrent.ExecutionException

286 Views Asked by At
  1. i have used the jersey implementation of a jaxrs but iam unable to the programme following is case where i am getting problem any idea help me

    in following programme i used the jersy 2.x implementaion of jaxrs i implemented the programme using jersey implemetation of jax-rs(restfull) 2 classes i have written instead of web.xml i used the class

    MyResource.java

    package com.rest.application;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.ws.rs.ApplicationPath;
    
    import com.rest.webservice.SampleService;
    @ApplicationPath("rest")
    public class MyResource {
        private Set s;
        public MyResource() {
            s=new HashSet();
            s.add(new SampleService());
        }
         public Set getSingletons() {
            return s;
        }
    }
    

    SampleService.java

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    
    @Path("sample")
    public class SampleService {
        @GET
        @Produces("text/html")
        @Path("{username}")
        public String sayHello(@PathParam("username")String s) {
            return "<font color='blue' size=8>Hello:" +s+ "</font>";
        }
        @GET
        @Produces("text/plain")
        public String sayBye() {
        return "Bye";
    }
    }
    

    i added the all jars needed to this programm still i am getting following error

    java.util.concurrent.ExecutionException:
    org.apache.catalina.LifecycleException:
    Failed to start component [StandardEngine[Catalina].
    StandardHost[localhost].StandardContext[/RestApp2]]
    Caused by: org.apache.catalina.LifecycleException:
    Failed to start component [StandardEngine[Catalina].
    StandardHost[localhost].StandardContext[/RestApp2]]
    

    like this same error is displaying everywhere

    when i changed the server tomcat 7 to 6 it is working but not displaying the output will anybody have any idea thanking you in advance

2

There are 2 best solutions below

0
On

As @MSD mentioned, your use of @ApplicationPath is incorrect. See the Jersey documentation on Application Deployment to see all the different deployment options, and how they work in different environments.

Basically, the easiest way put the @ApplicationPath on an empty Application class

@ApplicationPath("/rest")
public class MyApplication extends Application {}

This will scan the entire classpath for @Provider and @Path annotated classes, to register with the application. Though this may seem easier, the more common approach, when working with Jersey is to use it's ResourceConfig class, which is a subclass of Application. You can register packages, which will scan the packages and subpackages

@ApplicationPath("/rest")
public class MyApplication extends ResourceConfig {
    public MyApplication() {
        packages("com.my.packages");
    }
}

One benefit is that sometimes there will be third party dependencies that are annotated, but you don't want registered. To register individual classes just use register(...class) in the ResourceConfig.

Now the reason for the error in Tomcat 7 and not 6, is most likely because Tomcat 6 (servlet 2.5) does not have the sevlet pluggability mechanism, which uses the ServletContainerInitializer. The Jersey implementation of this initializer loads the application, looking for the @ApplicationPath on the Application subclass. If you're not in a 3.0 environment, this functionality will not work.

Note the Jersey initializer is included in the jersey-container-servlet jar. You can read more about it here

0
On

This says, @ApplicationPath("rest") may be applied only to the subclass of Application.

Can you share more on what are you trying to do and what is the complete stack trace. Are you using web.xml ?