Topic
  • 5 replies
  • Latest Post - ‏2012-11-15T06:27:33Z by Shawn_Jiang
SystemAdmin
SystemAdmin
2233 Posts

Pinned topic JAX-RS runtime does not scan classes for resources on start

‏2012-11-05T16:59:26Z |
I am trying to deploy a JAX-RS service on WASCE 3.0.0.2, and I want to have the web container automatically scan for REST resources on start in order to avoid having to register them manually.

The following classes are defined in a test.war file:

HelloWorld.java


@Path(
"/helloworld") 

public 

class HelloWorld 
{ @GET @Produces(MediaType.APPLICATION_XML) 

public Response helloWorld() 
{ String resp = 
"Hello World"; SomeData sd = 

new SomeData(); sd.setGreeting(resp); sd.setMs(System.currentTimeMillis()); 

return Response.ok(sd).build(); 
} 
}


SomeData.java


@XmlRootElement 

public 

class SomeData 
{   

private String greeting; 

private 

long ms;   
//Getters and setters omitted 
}


JaxRsApplication.java


@ApplicationPath(
"myrest") 

public 

class JaxRsApplication 

extends Application 
{ 
}


The JAX-RS application is started and available on http://localhost:8080/test/myrest but the resource http://localhost:8080/test/myrest/helloworld results in a http status 404.

If I amend the JaxRsApplication.java and manually register the resource, it works correctly:


@ApplicationPath(
"myrest") 

public 

class JaxRsApplication 

extends Application 
{ @Override 

public Set<Class<?>> getClasses() 
{ Set<Class<?>> classes = 

new HashSet<Class<?>>(); classes.add(HelloWorld.class); 

return classes; 
} 
}


According to various documentation, this last step should not be necessary as the container should be able to scan my classes for the @Path annotation. But it doesn't seem to work as expected - or am I missing something?
Updated on 2012-11-15T06:27:33Z at 2012-11-15T06:27:33Z by Shawn_Jiang
  • SystemAdmin
    SystemAdmin
    2233 Posts

    Re: JAX-RS runtime does not scan classes for resources on start

    ‏2012-11-07T06:27:39Z  
    A little extra info: This example works on WebSphere Application Server 8.5

    I hope this helps,

    Thanks
  • X75J_Li_Yanli
    X75J_Li_Yanli
    45 Posts

    Re: JAX-RS runtime does not scan classes for resources on start

    ‏2012-11-12T09:55:49Z  
    Hello,

    I recreated your sample and get the result: http://localhost:8080/Test/myrest/helloworld
    <someData>
    <greeting>Hello World</greeting>
    <ms>1352714154199</ms>
    </someData>

    Also attached the Test.war that I exported.
  • X75J_Li_Yanli
    X75J_Li_Yanli
    45 Posts

    Re: JAX-RS runtime does not scan classes for resources on start

    ‏2012-11-12T09:58:01Z  
    Please take Test.war as a reference.

    Attachments

  • SystemAdmin
    SystemAdmin
    2233 Posts

    Re: JAX-RS runtime does not scan classes for resources on start

    ‏2012-11-12T10:14:27Z  
    Please take Test.war as a reference.
    Yes, your .war will work.

    But it should be possible to make it work without overriding and adding the Helloworld.class in the application class, as the JAX-RS runtime is supposed to scan for any classes with the proper annotations.
  • Shawn_Jiang
    Shawn_Jiang
    154 Posts

    Re: JAX-RS runtime does not scan classes for resources on start

    ‏2012-11-15T06:27:33Z  
    Yes, your .war will work.

    But it should be possible to make it work without overriding and adding the Helloworld.class in the application class, as the JAX-RS runtime is supposed to scan for any classes with the proper annotations.
    Unfortunately, WASCE only supports the standard way to use Application for JAXRS configuration. I would suggest you to use Application to increase the portability of your app.

    Per JSR 311 Section 2.1 Configuration

    The resources and providers that make up a JAX-RS application are configured via an application-supplied
    subclass of Application. An implementation MAY provide alternate mechanisms for locating resource
    classes and providers (e.g. runtime class scanning) but use of Application is the only portable means of
    configuration.