Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

jersey - How can I enable MultiPartFeature?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...