5

My JAX-RS application has an extended Application class.

@ApplicationPath("/")
public class MyApplication extends Application {
    // empty; really empty
}

How can I enable org.glassfish.jersey.media.multipart.MultiPartFeature without modifying the class? Or without the necessity of registering all resource classes/packages?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
  • See also [Uploading file using Jersey over RESTfull service and The resource configuration is not modifiable?](http://stackoverflow.com/a/18256464/548473) – Grigory Kislin May 11 '17 at 22:37

2 Answers2

15

Not sure why you don't just use a ResourceConfig instead of an Application class. The only reason I can think of is portability, but the use of the Jersey specific multipart feature already breaks that portability.

But anyway, I'll try to answer this in the "most portable" way. What you can do is set a property, as you would in a web.xml. To set arbitrary properties, you can override

@Override
public Map<String, Object> getProperties() {}

in the Application subclass, and set the properties there.

@Override
public Map<String, Object> getProperties() {
    Map<String, Object> props = new HashMap<>();
    props.put("jersey.config.server.provider.classnames", 
            "org.glassfish.jersey.media.multipart.MultiPartFeature");
    return props;
}

This will maintain the classpath scanning for your resources and providers. The scanning is only disabled if you override getClasses() or getSingletons() (and return non-empty sets), but getProperties() is fine.

Another Option:

Create a Feature to wrap that feature, and let the feature be discovered, as seen here

Personally, I would...

Just use a ResourceConfig, as you're already breaking portability (what's a little more breakage :-)

@ApplicationPath("/")
public class AppConfig extends ResourceConfig {
    public AppConfig() {
        packages("packages.to.scan");
        register(MultiPartFeature.class);
    }
}
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Bravo!!! I actually used `packages(...).register(...)`. Then, suddenly, `jackson-provider` fails. And overriding `getProperties()` solved my problem. Bravo!!! – Jin Kwon Apr 09 '15 at 13:22
  • 1
    My guess is you're using [this Jackson provider](https://github.com/FasterXML/jackson-jaxrs-providers), which won't automatically register itself. And the classpath scanning picks up its `@Provider` annotations. Since the `ResourceConfig` doesn't do classpath scanning (instead package scanning), you could add the package to the `packages()`, like `packages("your.packages,com.fasterxml.jackson.jaxrs.json")`. This loads the Jackson ExceptionMapper as well as the readers and writers. – Paul Samsotha Apr 09 '15 at 13:35
  • 1
    Alternatively, you could use [this dependency](https://jersey.java.net/documentation/latest/media.html#json.jackson), which actually "wraps" the previous dependency in an "auto-dicoverable" feature (v 2.9+), so you don't need to explicitly register to package scan for it. Version 2.8- jusrt `register(JacksonFeature.class);` – Paul Samsotha Apr 09 '15 at 13:37
  • The dependency from my previous comment is jersey-media-json-jackson. [Here](https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/media.html#json.jackson) is the new link. – Paul Samsotha Jan 30 '21 at 15:50
0

For me worked like below:

        final ResourceConfig resourceConfig = new ResourceConfig(ApplicationConfig.class);
        resourceConfig.packages("com.econorma.rest");
        resourceConfig.register(MultiPartFeature.class);

        ServletHolder jerseyServlet  = new ServletHolder(new ServletContainer(resourceConfig));

This is ApplicationConfig class

@ApplicationPath("/")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> resources = new HashSet<Class<?>>();
        resources.add(MultiPartFeature.class);
        resources.add(EntryPoint.class);
        return resources;
    }

    @Override
    public Map<String, Object> getProperties() {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("jersey.config.server.provider.packages", "com.econorma.rest");
        return properties;
    }
}
Alessandro Mattiuzzi
  • 2,309
  • 2
  • 18
  • 24