java 将 Jetty 与 RESTEasy 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10082055/
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-10-30 23:31:46  来源:igfitidea点击:

Integrating Jetty with RESTEasy

javajakarta-eeservletsjettyresteasy

提问by rmoh21

Any links on how to integrate Jetty and RESTEasy? I am kinda stuck trying to configure RESTEasy with Jetty together....and there seems to be no credible help on the web.

关于如何集成 Jetty 和 RESTEasy 的任何链接?我在尝试将 RESTEasy 与 Jetty 一起配置时遇到了困难......而且网络上似乎没有可靠的帮助。

public static void main(String[] args) throws Exception
{
        Server server = new Server(8080);

        WebAppContext context = new WebAppContext();
        context.setDescriptor("../WEB-INF/web.xml");
        context.setResourceBase("../src/webapp");
        context.setContextPath("/");
        context.setParentLoaderPriority(true);

        server.setHandler(context);

        server.start();
        server.join();
}

My Web.xml is copied directly from: http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Installation_Configuration.html

我的 Web.xml 是直接从以下位置复制的:http: //docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Installation_Configuration.html

The error I get back is a HTTP 404 when I try to open up a link in my resource file. Everything looks reasonable on the surface, any suggestions?

当我尝试打开资源文件中的链接时,返回的错误是 HTTP 404。表面上看起来一切都很合理,有什么建议吗?

My resource file looks like:

我的资源文件看起来像:

package webapp;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("/*")
public class Resource {

   @GET
   public String hello() {
       return "hello";
   }


   @GET
   @Path("/books")
   public String getBooks() {
       return "books";
   }

   @GET
   @Path("/book/{isbn}")
   public String getBook(@PathParam("isbn") String id) {
       return "11123";
   }
}

This is the prints that I see when Jetty starts up:

这是我在 Jetty 启动时看到的打印:

2012-04-10 09:54:27.163:INFO:oejs.Server:jetty-8.1.1.v20120215 2012-04-10 09:54:27.288:INFO:oejw.StandardDescriptorProcessor:NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet 2012-04-10 09:54:27.319:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/C:/Users/xyz/Anotherproj1/src/webapp} 2012-04-10 09:54:27.319:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/C:/Users/xyz/Anotherproj1/src/webapp} 2012-04-10 09:54:27.381:INFO:oejs.AbstractConnector:Started [email protected]:8080

2012-04-10 09:54:27.163:INFO:oejs.Server:jetty-8.1.1.v20120215 2012-04-10 09:54:27.288:INFO:oejw.StandardDescriptorProcessor:NO JSP 没有找到支持org.apache.jasper.servlet.JspServlet 2012-04-10 09:54:27.319:INFO:oejsh.ContextHandler:started oejwWebAppContext{/,file:/C:/Users/xyz/Anotherproj1/src/webapp} 2012-04 -10 09:54:27.319:INFO:oejsh.ContextHandler:started oejwWebAppContext{/,file:/C:/Users/xyz/Anotherproj1/src/webapp} 2012-04-10 09:54:27.381:INFO:oejs。 AbstractConnector:Started [email protected]:8080

采纳答案by andih

The follwing works for me:

以下对我有用:

web.xml:

网页.xml:

<web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   <context-param>
      <param-name>resteasy.scan</param-name>
      <param-value>true</param-value>     
   </context-param>

   <context-param>
    <param-name>resteasy.resources</param-name>
    <param-value>webapp.Resource</param-value>
   </context-param>
    <context-param>
      <param-name>javax.ws.rs.core.Application</param-name>
      <param-value>webapp.MyApplicationConfig</param-value>
   </context-param>

   <!-- set this if you map the Resteasy servlet to something other than /*
   <context-param>
      <param-name>resteasy.servlet.mapping.prefix</param-name>
      <param-value>/resteasy</param-value>
   </context-param>
   -->
   <!-- if you are using Spring, Seam or EJB as your component model, remove the ResourceMethodSecurityInterceptor -->
   <context-param>
      <param-name>resteasy.resource.method-interceptors</param-name>
      <param-value>
         org.jboss.resteasy.core.ResourceMethodSecurityInterceptor
      </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>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

With

public class MyApplicationConfig extends Application {

    private static final Set<Class<?>> CLASSES;

    static {
        HashSet<Class<?>> tmp = new HashSet<Class<?>>();
        tmp.add(Resource.class);

        CLASSES = Collections.unmodifiableSet(tmp);
    }

    @Override
    public Set<Class<?>> getClasses(){

       return  CLASSES;
    }    


}

Resource

资源

package webapp;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/")
@Produces("text/plain")
public class Resource {

   @GET
   public String hello() {
       return "hello";
   }


   @GET
   @Path("/books")
   public String getBooks() {
       return "books";
   }

   @GET
   @Path("/book/{isbn}")
   public String getBook(@PathParam("isbn") String id) {
       return "11123";
   }
}

and Main Class

和主类

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap;

public class Main {
    public static void main(String[] args) throws Exception
    {
            Server server = new Server(8080);

            WebAppContext context = new WebAppContext();

            context.setDescriptor("./src/main/webapp/WEB-INF/web.xml");
            context.setResourceBase("./src/main/webapp");
            context.setContextPath("/");

            context.setParentLoaderPriority(true);            

            server.setHandler(context);

            server.start();
            server.join();
    }

}

回答by leoncc

To get RESTEasy and Jetty to work together withouta web.xml ensure you have a dependency on resteasy-servlet-initializer in your pom.xml.

要让 RESTEasy 和 Jetty 在没有web.xml 的情况下协同工作,请确保您在 pom.xml 中依赖于 resteasy-servlet-initializer。

This may help (JBoss RESTEasy documentation): https://docs.jboss.org/resteasy/docs/3.0.4.Final/userguide/html/Installation_Configuration.html#d4e111

这可能会有所帮助(JBoss RESTEasy 文档):https: //docs.jboss.org/resteasy/docs/3.0.4.Final/userguide/html/Installation_Configuration.html#d4e111

回答by Piotr Kochański

Are you sure that @Path("/*") is correct path. Try @Path("/") maybe this * is a problem. As far as I know path expressions does not accept regexps.

您确定 @Path("/*") 是正确的路径吗?试试 @Path("/") 也许这 * 是一个问题。据我所知,路径表达式不接受正则表达式。

EDIT

编辑

I was wrong, you can use regexps in @Path, at least RESTEasy supports that.

我错了,你可以在@Path 中使用正则表达式,至少RESTEasy 支持