如何启用MultiPartFeature? [英] How can I enable MultiPartFeature?
问题描述
我的JAX-RS应用程序具有扩展的Application类.
My JAX-RS application has an extended Application class.
@ApplicationPath("/")
public class MyApplication extends Application {
// empty; really empty
}
如何在不修改类的情况下启用org.glassfish.jersey.media.multipart.MultiPartFeature
?还是不需要注册所有资源类/程序包?
How can I enable org.glassfish.jersey.media.multipart.MultiPartFeature
without modifying the class? Or without the necessity of registering all resource classes/packages?
推荐答案
不确定为什么不只使用ResourceConfig
而不是Application
类.我能想到的唯一原因是可移植性,但是使用Jersey特定的多部分功能已经破坏了可移植性.
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.
但是无论如何,我将尝试以最可移植"的方式回答这个问题.您可以像在web.xml中一样设置属性.要设置任意属性,您可以覆盖
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() {}
Application
子类中的
,并在那里设置属性.
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;
}
这将维护您的资源和提供程序的类路径扫描.仅当您覆盖getClasses()
或getSingletons()
(并返回非空集)时,才禁用扫描,但是getProperties()
可以.
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.
另一个选项:
创建一个Feature
来包装 that 功能,并让该功能被发现,如在此处看到
Create a Feature
to wrap that feature, and let the feature be discovered, as seen here
我个人而言...
只需使用ResourceConfig
,因为您已经破坏了可移植性(还有点破损:-)
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);
}
}
这篇关于如何启用MultiPartFeature?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!