Java 如何部署 JAX-RS 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2072295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:16:30  来源:igfitidea点击:

How to deploy a JAX-RS application?

javadeploymentservletsjax-rs

提问by deamon

The JAX-RS 1.1 specification says on page 6:

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

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

javax.ws.rs.core.Application

如果不存在 Application 子类,则添加的 servlet 必须命名为:

javax.ws.rs.core.Application

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

如果存在 Application 子类并且已经定义了一个 servlet,它具有一个名为:

javax.ws.rs.Application

Again, what is "a servlet" here?

同样,这里的“servlet”是什么?

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.

如果存在的应用程序子类未被现有的 servlet 处理,那么由 ContainerInitializer 添加的 servlet 必须以应用程序子类的完全限定名称命名。

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

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

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?

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

How to deploy on a Servlet container?

如何部署在 Servlet 容器上?

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?

网络中有大量令人困惑的配置选项。在 web.xml 中查看带有上下文参数的示例(对我不起作用!)。部署 JAX-RS 应用程序的首选方法是什么?

采纳答案by Bryant Luk

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

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

The simplest is:

最简单的是:

<?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>

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

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

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

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

<?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>

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

如果您希望只在 URL 上返回特定的@Path/@Provider类集,您可能想要执行上述操作(因此您可以使用上面不同的 URL 模式创建第二个 MyApplication2)。

You can also skip the whole web.xmlaltogether and just annotate your MyApplicationclass wih @ApplicationPathwhich will serve as the URL pattern. I would recommend keeping the web.xmlin any case because you will probably have to add other information about the web application there anyway.

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

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

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

回答by Robert Wilson

As I said in the comment above, it all depends on the framework you want to use.

正如我在上面的评论中所说,这完全取决于您要使用的框架。

http://syrupsucker.blogspot.com/2008/10/deploying-jersey-in-tomcat-60.htmlfor Jersey http://syrupsucker.blogspot.com/2008/10/deploying-resteasy-in-tomcat-60.htmlfor RESTeasy

http://syrupsucker.blogspot.com/2008/10/deploying-jersey-in-tomcat-60.html泽西岛 http://syrupsucker.blogspot.com/2008/10/deploying-resteasy-in-tomcat-60 .html用于 RESTeasy

As far as I know, JAX-RS does not contain a specification for deployment.

据我所知,JAX-RS 不包含部署规范。

回答by PrasadO

With Servlet3.0, follow this. This works for me.

使用 Servlet3.0,请遵循此。这对我有用。

<servlet>
    <description>JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>your.restsrv.config.RESTConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <enabled>true</enabled>
    <async-supported>false</async-supported>
</servlet>
<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>
<servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

回答by Matthieu Lambert

With WAS 8.5, I change the web.xml to add:

在 WAS 8.5 中,我更改了 web.xml 以添加:

<servlet>
    <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.tada.rest.RestApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

My RestApplication look like :

我的 RestApplication 看起来像:

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

public class RestApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> sets = new HashSet<Class<?>>();
        sets.add(RestService.class);
        return sets;
    }
}

My RestService looks like

我的 RestService 看起来像

@Path("/tada")
public class RestService {
    @GET
    public String getSomething() {
        return "tada";
    }
}

And I add in the pom.xml the dependency:

我在 pom.xml 中添加了依赖项:

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
</dependency>