2012-04-10 34 views
9

Bất kỳ liên kết nào về cách tích hợp Jetty và RESTEasy? Tôi đang cố gắng cố gắng để cấu hình RESTEasy với Jetty với nhau .... và dường như không có sự trợ giúp đáng tin cậy trên web.Tích hợp Jetty với RESTEasy

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 của tôi được sao chép trực tiếp từ: http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Installation_Configuration.html

Các lỗi tôi nhận được lại là một HTTP 404 khi tôi cố gắng mở một liên kết trong tập tin tài nguyên của tôi. Mọi thứ có vẻ hợp lý trên bề mặt, mọi gợi ý?

tập tin tài nguyên của tôi trông giống như:

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

Đây là bản in mà tôi thấy khi Jetty khởi động:

2012/04/10 09: 54: 27,163: INFO: oejs.Server: jetty-8.1.1.v20120215 2012-04-10 09: 54: 27.288: THÔNG TIN: oejw.StandardDescriptorProcessor: KHÔNG hỗ trợ JSP cho /, không tìm thấy org.apache.jasper.servlet.JspServlet 2012-04-10 09:54 : 27.319: THÔNG TIN: oejsh.ContextHandler: bắt đầu oejwWebAppContext {/, tệp:/C:/Người dùng/xyz/Anotherproj1/src/webapp} 2012-04-10 09: 54: 27.319: THÔNG TIN: oejsh.ContextHandler: bắt đầu oejw WebAppContext {/, tệp:/C:/Người dùng/xyz/Anotherproj1/src/webapp} 2012-04-10 09:54: 27.381: THÔNG TIN: oejs.AbstractConnector: Bắt đầu [email protected]: 8080

+0

Lúc đầu, điều này có vẻ chính xác. Bạn đang sử dụng phiên bản Jetty nào. Có thông báo lỗi nào không? Vấn đề chính xác của bạn là gì? – andih

+0

@andih Lỗi cơ bản là HTTP 404 khi tôi cố mở một liên kết trong tệp tài nguyên của mình. – rmoh21

+0

@andih Tôi đang sử dụng Jetty 8.1.1 – rmoh21

Trả lời

6

Các tác phẩm follwing cho tôi:

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> 

Với

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

và Class Main

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

} 
+1

Cảm ơn rất nhiều nó đã làm việc tuyệt vời! Chỉ có một điều có một lỗi đánh máy trong trang web của bạn.xml file - nó nên được "ResteasyBootstrap" tại chỗ tắt "ResteasyBootstap" - 'r' là mất tích. – rmoh21

0

Bạn có chắc chắn rằng @Path ("/ *") là đường dẫn đúng hay không. Hãy thử @Path ("/") có thể đây * là một vấn đề. Theo như tôi biết biểu thức đường dẫn không chấp nhận regexps.

EDIT

tôi đã sai, bạn có thể sử dụng regexps trong @Path, ít nhất RESTEasy supports that.

+0

Đã thử rằng nó không hoạt động tốt. Tôi nhận được một 404 không tìm thấy trong trình duyệt của tôi khi tôi nhập vào localhost: 8080/ – rmoh21