0
votes

I've got the start of a RESTful web service using JAX-RS and Jersey that exposes two resources: SessionResource and ItemResource. Only one of these, unfortunately, is exposed by the web service.

Details:

  • configuration is done w/ a class that extends javax.ws.rs.core.Application (created automatically by Netbeans 7). the class doesn't contain any configuration information other than a @ApplicationPath() annotation.
  • no web.xml file

Questions:

  • What am I missing?
  • is there value to having an application class? can i get away w/ just a web.xml file for configuration?
  • sometimes i've noticed that changes made in the IDE aren't published to apache. what is the most reliable way to do so?
2

2 Answers

1
votes

One of multiple solutions is to override getClasses() of your JAX-RS application.

@ApplicationPath("/")
public class MyApplication extends Application
{
    @Override
    public Set<Class<?>> getClasses()
    {
        return new HashSet<Class<?>>()
        {
            {
                add(ResourceA.class);
                add(ResourceB.class);
            }
        };
    }
}

As I learned it is better not to rely on the automatic detection of resources and providers. Sometimes it can have sideeffects if you have more than one JAX-RS application.

0
votes

You can do this in your web.xml and then you shouldn't need the Application class.

<servlet>
  <servlet-name>JerseyStartup</servlet-name>
  <servlet-class>[fully qualified name of a class that extends ServletContainer]</servlet-class>
  <init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>com.yourCompany</param-value>
  </init-param>
</servlet>

Then it will automatically detect all classes in com.yourCompany and its subpackages, which are annotated with @Path, and treat them as resources.