将Jetty与RESTEasy集成 [英] Integrating Jetty with RESTEasy

查看:88
本文介绍了将Jetty与RESTEasy集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关如何集成Jetty和RESTEasy的任何链接?我有点想和Jetty一起配置RESTEasy ....而且网上似乎没有可靠的帮助。

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.

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();
}

我的Web.xml直接复制自:
http://docs.jboss.org/resteasy/docs/ 1.0.0.GA/userguide/html/Installation_Configuration.html

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

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

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?

我的资源文件如下:

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";
   }
}

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

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

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:没有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:启动oejwWebAppContext {/,文件:/ C:/ Users / xyz / Anotherproj1 / src / webapp} 2012-04-10 09:54:27.381:INFO:oejs.AbstractConnector:已启动SelectChannelConnector@0.0.0.0: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 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 SelectChannelConnector@0.0.0.0:8080

推荐答案

以下内容适用于我:

web.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>

使用

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;
    }    


}

资源

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";
   }
}

和主类

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();
    }

}

这篇关于将Jetty与RESTEasy集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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