1

In the case of (server-side) Application configuration, we can easily scan packages for JAX-RS resource classes with

 ResourceConfig rCfg = new ResourceConfig();
 rCfg.packages("com.my.package", ...);    

and then initializing the application using the ResourceConfig as the Application object.

Using the client-side though, it is not clear how to perform package scanning.

We can do

  Resource.from(SomeResourceClass.class);

to get resources if we know the class name. In my case we don't know the class names, and we'd like to get the classes based on their @Path. If we knew all of the class names up front, we could use repeated calls to Resource.from() to get all the resources, then index them by path, then lookup the paths as necessary.

But we don't know all the class names up front. Is there some way to get all of the Resource in a particular package, or even better scan the entire classpath for them - all without initializing a (server-side) Application?

BadZen
  • 4,083
  • 2
  • 25
  • 48
  • Alternatively, if there is a feature we could initialize as a stand-alone or stripped down Application (ie. without all the Feature that make up a server), and will do just resource resolution based on the given path, that would also work... – BadZen Jun 06 '17 at 05:13

1 Answers1

1

For package scanning, you can use the PackageNamesScanner. Here is an example

public static void main(String... args) throws Exception {
    final String[] pkgs = {"com.example.jersey"};
    final boolean recursive = true;

    final AnnotationAcceptingListener asl = new AnnotationAcceptingListener(Path.class);
    final PackageNamesScanner scanner = new PackageNamesScanner(pkgs, recursive);
    while (scanner.hasNext()) {
        final String next = scanner.next();
        if (asl.accept(next)) {
            try (final InputStream in = scanner.open()) {
                asl.process(next, in);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
    asl.getAnnotatedClasses().forEach(System.out::println);
}

Unfortunately, I don't think there is an "ClassPathScanner". Classpath scanning is only used for servlet environments (and Jersey can be run outside of servlet environments), so there is a one-off implementation off the classpath scanning in the WebAppResourcesScanner, but it would not work in your case, as it scans specific paths.

You can also look at some of the general posts regarding scanning the classpath for annotated classes, like:

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Perfect, I was able to these classes with `ResourceModel.Builder` to scan the packages for a resource model. – BadZen Jun 06 '17 at 14:05