RESTEasy常用部署方式和配置
这里的文章参考RESTEasy官方文档,只是对相应的部分进行了汉化,英语不错的同学可以直接参考官方文档。以下配置代码都是在web.xml中。
>>最小配置模式:
<web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>Resteasy</servlet-name> <servlet-class> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher </servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>org.jboss.resteasy.examples.service.LibraryApplication</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Resteasy</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
以上代码中的
<init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.restfully.shop.services.ShoppingApplication</param-value> </init-param>
可以删除,然后在web.xml中添加<context-param>。如:
<context-param> <param-name>javax.ws.rs.Application</param-name> <param-value>org.jboss.resteasy.examples.service.LibraryApplication</param-value> </context-param>
>>修改url-pattern。这里对应的url-pattern是/*,如果需要改成/aaa/bbb/*,则需要相应的改变配置:
<context-param> <param-name>javax.ws.rs.Application</param-name> <param-value>org.jboss.resteasy.examples.service.LibraryApplication</param-value> </context-param> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/aaa/bbb</param-value> </context-param> <servlet> <servlet-name>Resteasy</servlet-name> <servlet-class> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher </servlet-class> </servlet> <servlet-mapping> <servlet-name>Resteasy</servlet-name> <url-pattern>/aaa/bbb/*</url-pattern> </servlet-mapping>
>>通过Listener启动RESTEasy。这种情况通常用于想在系统启动时立即使用RESTEasy的功能。常见的情况是在另一个Listener中要使用RESTEasy。
<context-param> <param-name>javax.ws.rs.Application</param-name> <param-value>org.jboss.resteasy.examples.service.LibraryApplication</param-value> </context-param> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/aaa/bbb</param-value> </context-param> <listener> <listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap </listener-class> </listener> <servlet> <servlet-name>Resteasy</servlet-name> <servlet-class> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher </servlet-class> </servlet> <servlet-mapping> <servlet-name>Resteasy</servlet-name> <url-pattern>/aaa/bbb/*</url-pattern> </servlet-mapping>
>>使用Filter配置RESTEasy。通常使用servlet配置时,在相同的路径下不能再有静态资源文件。因为那样的请求会被当作一个service路径来处理。同时如果指定路径下没有相应的service对应,RESTEasy还能委派一个基础的service来处理。
<filter> <filter-name>Resteasy</filter-name> <filter-class> org.jboss.resteasy.plugins.server.servlet.FilterDispatcher </filter-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.restfully.shop.services.ShoppingApplication</param-value> </init-param> </filter> <filter-mapping> <filter-name>Resteasy</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
>>在Jboss7中配置。由于RESTEasy 已经完全集成在Jboss7中,所以只需要保证项目中有一个web.xml文件即可,空文件都行。同样上面的一些<context-param>可以在此web.xml中使用。但注意的是,因为这里没有使用JAX-RS Servlet来映射URL地址,所以需要额外使用@ApplicationPath来配置。
import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/root-path") public class MyApplication extends Application { }
这里如果你没有指定一些classes 或者 singletons,系统会自动扫描所有包含resource 和provider JAX-RS 注解的类。
一个完整的Application实现通常是这样的:
import javax.ws.rs.core.Application; import java.util.HashSet; import java.util.Set; public class ShoppingApplication extends Application { private Set<Object> singletons = new HashSet<Object>(); private Set<Class<?>> empty = new HashSet<Class<?>>(); public ShoppingApplication() { singletons.add(new CustomerResource()); } @Override public Set<Class<?>> getClasses() { return empty; } @Override public Set<Object> getSingletons() { return singletons; } }
>>常用注册resources的模式:
<!-- 1. 使用手动指定Application的模式。 <context-param> <param-name>javax.ws.rs.Application</param-name> <param-value>org.jboss.resteasy.examples.service.LibraryApplication</param-value> </context-param> --> <!-- 2. 使用自动Scan的模式 <context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param> --> <!-- 3. 使用手动注册resources的模式 --> <context-param> <param-name>resteasy.resources</param-name> <param-value>org.jboss.resteasy.examples.service.MyFormService,org.jboss.resteasy.examples.service.Library</param-value> </context-param>
>>常用的<context-param>配置参数和说明:
resteasy.servlet.mapping.prefix | no default | If the url-pattern for the Resteasy servlet-mapping is not /* |
resteasy.scan | false | Automatically scan WEB-INF/lib jars and WEB-INF/classes directory for both @Provider and JAX-RS resource classes (@Path, @GET, @POST etc..) and register them |
resteasy.scan.providers | false | Scan for @Provider classes and register them |
resteasy.scan.resources | false | Scan for JAX-RS resource classes |
resteasy.providers | no default | A comma delimited list of fully qualified @Provider class names you want to register |
resteasy.use.builtin.providers | true | Whether or not to register default, built-in @Provider classes. (Only available in 1.0-beta-5 and later) |
resteasy.resources | no default | A comma delimited list of fully qualified JAX-RS resource class names you want to register |
resteasy.jndi.resources | no default | A comma delimited list of JNDI names which reference objects you want to register as JAX-RS resources |
javax.ws.rs.Application | no default | Fully qualified name of Application class to bootstrap in a spec portable way |
resteasy.media.type.mappings | no default | Replaces the need for an Accept header by mapping file name extensions (like .xml or .txt) to a media type. Used when the client is unable to use a Accept header to choose a representation (i.e. a browser). See JAX-RS Content Negotiation chapter for more details. |
resteasy.language.mappings | no default | Replaces the need for an Accept-Language header by mapping file name extensions (like .en or .fr) to a language. Used when the client is unable to use a Accept-Language header to choose a language (i.e. a browser). See JAX-RS Content Negotiation chapter for more details |
resteasy.document.expand.entity.references | true | Expand external entities in org.w3c.dom.Document files |