如何部署 JAX-RS 应用程序? [英] How to deploy a JAX-RS application?

查看:35
本文介绍了如何部署 JAX-RS 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JAX-RS 1.1 规范在第 6 页上说:

The JAX-RS 1.1 specification says on page 6:

如果不存在应用程序子类添加的 servlet 必须命名为:

If no Application subclass is present the added servlet MUST be named:

javax.ws.rs.core.Application

添加的servlet是什么?它可能是一个任意的 servlet?

What is the added servlet? Could it be an arbitrary servlet?

如果存在应用程序子类并且已经定义了一个 servlet有一个 servlet 初始化参数命名:

If an Application subclass is present and there is already a servlet defined that has a servlet initialization parameter named:

javax.ws.rs.Application

再说一次,这里的servlet"是什么?

Again, what is "a servlet" here?

如果存在应用程序子类没有被处理的现有的 servlet 然后是 servlet由 ContainerInitializer 添加必须以完全限定名称命名应用程序子类.

If an Application subclass is present that is not being handled by an existing servlet then the servlet added by the ContainerInitializer MUST be named with the fully qualified name of the Application subclass.

ContainerInitializer 添加的 servlet"是否意味着 servlet 是自动添加的?配置会是什么样子?

Does "the servlet added by the ContainerInitializer" mean that the servlets is added automatically? How would a configuration look like?

目前我既不使用 Application 类也不使用 web.xml 并且它可以工作(使用 GlassFish 3.1).这种部署机制是否需要完整的类路径扫描,这对于大型库来说可能会很慢?

At the moment I use neither an Application class nor a web.xml and it works (with GlassFish 3.1). Does this deployment mechanism require a full class path scan, which could be slow with big libraries?

如何部署在 Servlet 容器上?

How to deploy on a Servlet container?

网络中有大量令人困惑的配置选项.请参阅此 带有 web.xml 中的上下文参数的示例(不适用于我!).部署 JAX-RS 应用程序的首选方法是什么?

There is a confusing number of configuration options around in the web. See this example with context params in the web.xml (doesn't work for me!). What is the preferred way to deploy a JAX-RS application?

推荐答案

有许多选项可用于部署到 Java EE 6 容器(更具体地说是 Servlet 3.0 实现):

There are a number of options for deploying into a Java EE 6 container (more specifically a Servlet 3.0 implementation):

最简单的是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd" version="3.0">
    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

然后在您的 Web 应用程序中找到的所有 @Path@Provider 类都将在默认"JAX-RS 应用程序中可用,其 servlet URL 模式为 <代码>"/rest/*".

Then all the @Path and @Provider classes found in your web application will be available in the "default" JAX-RS application with a servlet URL pattern of "/rest/*".

如果你有一个或多个扩展 javax.ws.rs.core.Application 的类,你可以这样指定:

If you have one or more classes that extends javax.ws.rs.core.Application, you can specify like so:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd" version="3.0">
    <servlet>
        <servlet-name>com.example.jaxrs.MyApplication</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>com.example.jaxrs.MyApplication</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

如果您只想在 URL 上返回特定的 @Path/@Provider 类集,您可能想要执行上述操作(因此您可以有第二个MyApplication2 具有上述不同的 URL 模式).

You may want to do the above in case you wish to only return specific sets of @Path/@Provider classes on a URL (so you could have a second MyApplication2 with a different URL pattern above).

您也可以完全跳过整个 web.xml,只用 @ApplicationPath 注释您的 MyApplication 类,它将用作 URL 模式.我建议在任何情况下都保留 web.xml,因为无论如何您可能必须在那里添加有关 Web 应用程序的其他信息.

You can also skip the whole web.xml altogether and just annotate your MyApplication class wih @ApplicationPath which will serve as the URL pattern. I would recommend keeping the web.xml in any case because you will probably have to add other information about the web application there anyway.

如果您想知道 servlet-class 是从哪里来的,它会被环境自动添加进来.您可以通过查看 Servlet 3.0 ServletContext 获得一个想法.

If you're wondering where the servlet-class comes from, it is automatically added in by the environment. You can get an idea by looking at the Servlet 3.0 ServletContext.

这篇关于如何部署 JAX-RS 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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