使用哪个初始化参数:jersey.config.server.provider.packages或javax.ws.rs.Application? [英] which init-param to use: jersey.config.server.provider.packages or javax.ws.rs.Application?

查看:121
本文介绍了使用哪个初始化参数:jersey.config.server.provider.packages或javax.ws.rs.Application?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将JAX-RS Web服务部署到Tomcat Servlet容器.

I am deploying JAX-RS web services to a Tomcat servlet container.

我已经看到了使用以下两种方法之一表示在web.xml文件中的资源的代码示例:

I have seen code examples that use either of the following two methods of indicating the resources in the web.xml file:

  <servlet>
      <servlet-name>Jersey Web Application</servlet-name>
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
      <init-param>
          <param-name>jersey.config.server.provider.packages</param-name>
          <param-value>com.example</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>

...希望资源驻留在com.example包中的位置,我想是通过Java RTTI发现的.

...where the resources are expected to reside in the com.example package and I suppose are discovered by means of Java RTTI.

<servlet>
 <servlet-name>jersey-serlvet</servlet-name>
 <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
   <init-param>
           <param-name>javax.ws.rs.Application</param-name>
           <param-value>full.qualified.name.to.MyApplication</param-value>
   </init-param>
 <load-on-startup>1</load-on-startup>
</servlet> 

...,其中MyApplication类明确标识资源类:

... where the MyApplication class identifies explicitly the resource classes:

public class MyApplication extends javax.ws.rs.core.Application {
   public Set<Class<?>> getClasses() {
      Set<Class<?>> s = new HashSet<Class<?>>();
      s.add(ResourceA.class);
      return s;
}

使用一种方法相对于另一种方法纯粹是出于口味和配置方面的问题,需要考虑哪些权衡因素?就个人而言,我更喜欢方法2 提供的更细粒度的控制,但是maven Jersey 2.7原型:

Is using the one versus the other method purely a matter of taste and configuration effort and what are some trade-offs to consider? Personally, I prefer the more fine-grained control offered by method 2, however the maven Jersey 2.7 archetype:

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp \
            -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
            -DgroupId=com.example -DartifactId=simple-service-webapp -Dpackage=com.example \
            -DarchetypeVersion=2.7

...正在使用方法1 ,这让我开始思考.

... is using method 1 and that got me thinking.

推荐答案

方法1 (使用servlet的初始化参数jersey.config.server.provider.packages):是特定于Jersey的,仅在包中显示.它不能在不同的JAX-RS实现之间移植.您可以在想要限制考虑的JAX-RS资源类/应用程序的场景中使用它.

Method 1 (using servlet's init param jersey.config.server.provider.packages): is Jersey specific and looks only in packages. It is not portable between different JAX-RS implementations. You can use it in scenarios when you want to restrict the considered JAX-RS Resource classes/applications.

方法2 (使用servlet的初始化参数javax.ws.rs.Application):任何JAX-RS实现都必须支持此部署选项,因此具有可移植性(尽管如果您切换至RestEasy之类的另一个JAX-RS实现,则将不得不更改servlet的类).此选项提供了更多的粒度(您可以完全选择要考虑的类,而不仅仅是整个软件包).缺点:您必须编写更多代码.

Method 2 (using servlet's init param javax.ws.rs.Application): any JAX-RS implementation MUST support this deployment option, thus portable (although if you switch to another JAX-RS implementation like RestEasy, you will have to change the servlet's class). This option offers more granularity (you can select exactly the classes to be considered, not only entire packages). The disadvantage: you have to write more code.

方法3 (在您可能已经部署过的Servlet版本3容器中):仅定义您的JAX-RS应用程序,而无需任何Servlet(请检查

Method 3 (in a Servlet ver. 3 Container, where you probably already deploy): defining only your JAX-RS applications without any servlets (check Deployment using web.xml descriptor) is probably the best way (it is also portable between JAX-RS implementations and you can change the JAX-RS implementation without a change in web.xml), if you have an explicitly declared JAX-RS Application (which you have).

方法4 如果要将战争存档中的所有类部署到servlet容器3中(没有显式定义的JAX-RS应用程序),则也可以通过可移植的方式进行.在此处检查:没有Application子类的JAX-RS应用程序

Method 4 If you want to deploy all classes from your war archive in a servlet container 3 (without an explicitly-defined JAX-RS application), you can do that also in a portable way. Check it here: JAX-RS application without an Application subclass

这篇关于使用哪个初始化参数:jersey.config.server.provider.packages或javax.ws.rs.Application?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆