2012-04-10 19 views
9

Eventuali collegamenti su come integrare Jetty e RESTEasy? Sono un po 'bloccato cercando di configurare RESTEasy con Jetty insieme ... e sembra che non ci sia un aiuto credibile sul web.Integrazione di Jetty con 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(); 
} 

mio Web.xml viene copiato direttamente da: http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Installation_Configuration.html

L'errore che ottengo sul retro è un HTTP 404 quando si tenta di aprire un collegamento nel mio file di risorse. Tutto sembra ragionevole in superficie, qualche suggerimento?

mio file di risorse appare come:

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

Questa è la stampa che vedo quando Jetty si avvia:

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 supporto JSP per /, non trovato org.apache.jasper.servlet.JspServlet 2012-04-10 09:54 : 27.319: INFO: oejsh.ContextHandler: avviato oejwWebAppContext {/, file:/C:/Users/xyz/Anotherproj1/src/webapp} 2012-04-10 09: 54: 27.319: INFO: oejsh.ContextHandler: avviato oejw WebAppContext {/, file:/C:/Users/xyz/Anotherproj1/src/webapp} 2012-04-10 09:54: 27.381: INFO: oejs.AbstractConnector: avviato [email protected]: 8080

+0

In un primo glace questo sembra corretto. Quale versione di Jetty stai usando. Ci sono messaggi di errore? Qual è esattamente il tuo problema? – andih

+0

@andih L'errore è essenzialmente un HTTP 404 quando provo ad aprire un collegamento nel mio file di risorse. – rmoh21

+0

@andih Sto usando il Jetty 8.1.1 – rmoh21

risposta

6

Le opere follwing per me:

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> 

Con

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

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

} 
+1

Grazie mille ha funzionato alla grande! Solo una cosa c'è un errore di battitura nel tuo web.file xml - dovrebbe essere "ResteasyBootstrap" in posizione off "ResteasyBootstap" - manca la "r". – rmoh21

0

Sei sicuro che @Path ("/ *") è il percorso corretto. Prova @Path ("/") forse questo * è un problema. Per quanto ne so, le espressioni di percorso non accettano espressioni regolari.

EDIT

mi sbagliavo, è possibile utilizzare espressioni regolari in @Path, almeno RESTEasy supports that.

+0

Provato che non fa nulla di buono. Ottengo un 404 non trovato nel mio browser quando inserisco localhost: 8080/ – rmoh21