如何使用独立 Jetty 服务器配置 Resteasy?

How to Configure Resteasy With A Standalone Jetty Server?

我正在尝试将应用程序部署到独立的 Jetty 9.4 服务器。我正在为我的 Web 服务使用 resteasy,但到目前为止,我一直在努力寻找清晰的示例或教程来准确解释我需要如何配置所有内容才能使其正常工作。到目前为止,我发现的每个示例似乎都是针对 jboss 服务器、不同的休息框架(如 Jersey)或嵌入式码头服务器。我试图从我能够挖掘但没有成功的几部分中拼凑出一些东西。当我尝试向我的 Web 服务发出 GET 请求时,我最终遇到了 404 错误。如果能帮助我指明正确的方向,我将不胜感激。

这里有一些文件来演示我当前的配置:

码头-web.xml

<Configure id="eyerep-data" class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/eyerep-data</Set>
    <Set name="war"><SystemProperty name="jetty.monitorDir" default="./webapps" />/eyerep-data.war</Set>
</Configure>

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  metadata-complete="false" version="3.0"
>
  <context-param>
    <param-name>appName</param-name>
    <param-value>eyerep</param-value>
  </context-param>
  <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
  </context-param>
  <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/api</param-value>
  </context-param>     
  <context-param>
    <param-name>resteasy.guice.modules</param-name>
    <param-value>com.google.inject.servlet.ServletModule,com.tura.eyerep.guice.EyerepGuiceModule</param-value>
  </context-param>
  <context-param>
    <param-name>resteasy.guice.holder</param-name>
    <param-value>com.tura.eyerep.guice.Guice</param-value>
  </context-param>
 
  <listener>
    <listener-class>com.tura.eyerep.guice.resteasy.MyGuiceResteasyBootstrapServletContextListener
    </listener-class>
  </listener>
  <listener>
    <listener-class>com.tura.eyerep.servlet.StartupShutdownListener</listener-class>
  </listener>
 
  <filter>
    <filter-name>GuiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
  </filter>
  <servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher</servlet-class>
    <async-supported>true</async-supported>
  </servlet>
 
  <filter-mapping>
    <filter-name>GuiceFilter</filter-name>
    <servlet-name>Resteasy</servlet-name>
  </filter-mapping>
  <servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/api/*</url-pattern>
    <url-pattern>/auth/*</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>10</session-timeout>
  </session-config>     
</web-app>

我的服务Class:

@Path("/api")
public class ExportDataApi {
     @GET
     @Path("/data/export/repName/{repId}")
     public Response getRepName(@PathParam("repId") String repId)
     {
       ...
     }
}

我想我终于找到了答案。我将这个项目基于一个使用旧版本 jetty 9 的应用程序。据我所知,在某些时候 jetty.monitorDir 已从 jetty 的命令行选项中删除。由于该选项无效,它会回退到默认值,这就是为什么当我将 war 直接放在 webapps/ 下时我的应用程序运行的原因。

要使其正常运行,我需要进行两项更改。在 jetty-web.xml 中,我需要将 war 配置更改为:

<Set name="war"><SystemProperty name="jetty.base" default="./webapps" />/eyerep-data/eyerep-data.war</Set>

然后在 start.ini 中,我必须找到 'deploy' 模块的部分,取消注释 jetty.deploy.monitoredDir 的设置,并将其更改为

jetty.deploy.monitoredDir=webapps/eyerep-data