问题描述
使用ResourceConfig相对于Application有什么优点(因为ResourceConfig扩展了Application).
使用ResourceConfig
@ApplicationPath("/")
public class MyApplication extends ResourceConfig {
public MyApplication() {
super(MultiPartResource.class, MultiPartResource.class, MultiPartFeature.class);
}
}
使用应用程序
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register resources and features
classes.add(MultiPartFeature.class);
classes.add(MultiPartResource.class);
classes.add(LoggingFilter.class);
return classes;
}
}
在帖子 Jersey 2多部分表单数据的注入源中 @Arul Dhesiaseelan回答说要添加同时使用MultiPartFeature来在服务器端启用该功能.有人可以解释.
从文档中: https://jersey.java.net/documentation/latest/deployment.html
与应用程序相比,ResourceConfig提供了简化JAX-RS组件注册的高级功能,例如,在提供的类路径或一组包名称中扫描根资源和提供程序类,等等.所有JAX-RS组件手动注册或在扫描过程中找到的类会自动添加到getClasses返回的一组类中."What is the advantages of using ResourceConfig over Application(since ResourceConfig extends Application).
using ResourceConfig
@ApplicationPath("/")
public class MyApplication extends ResourceConfig {
public MyApplication() {
super(MultiPartResource.class, MultiPartResource.class, MultiPartFeature.class);
}
}
using Application
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register resources and features
classes.add(MultiPartFeature.class);
classes.add(MultiPartResource.class);
classes.add(LoggingFilter.class);
return classes;
}
}
In the post Jersey 2 injection source for multipart formdata @Arul Dhesiaseelan answered to add the MultiPartFeature to both to enable that feature on the server side.Could someone explain.
from the documentation: https://jersey.java.net/documentation/latest/deployment.html
"Compared to Application, the ResourceConfig provides advanced capabilities to simplify registration of JAX-RS components, such as scanning for root resource and provider classes in a provided classpath or a in a set of package names etc. All JAX-RS component classes that are either manually registered or found during scanning are automatically added to the set of classes that are returned by getClasses. "
这篇关于ResourceConfig和应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!