2012-04-10 35 views
9

¿Algún enlace sobre cómo integrar Jetty y RESTEasy? Estoy un poco atascado tratando de configurar RESTEasy con Jetty juntos ... y parece que no hay ayuda creíble en la web.Embarcadero de integración 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(); 
} 

Mi Web.xml se copia directamente de: http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Installation_Configuration.html

El error que consigo de vuelta es un HTTP 404 cuando intento abrir un enlace en mi archivo de recursos. Todo parece razonable en la superficie, ¿alguna sugerencia?

Mi archivo de recursos se ve así:

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

Ésta es la impresiones que veo cuando embarcadero se pone en marcha:

2012-04-10 09: 54: 27.163: INFO: oejs.Server: jetty-8.1.1.v20120215 2012-04-10 09: 54: 27.288: INFORMACIÓN: oejw.StandardDescriptorProcessor: NO JSP Support for /, no encontró 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: INFORMACIÓN: oejsh.ContextHandler: started oejw WebAppContext {/, file:/C:/Users/xyz/Anotherproj1/src/webapp} 2012-04-10 09:54: 27.381: INFO: oejs.AbstractConnector: Comenzó [email protected]: 8080

+0

A primera vista esto parece correcto. ¿Qué versión de Jetty estás usando? ¿Hay algún mensaje de error? ¿Cuál es exactamente tu problema? – andih

+0

@andih El error básicamente es un HTTP 404 cuando trato de abrir un enlace en mi archivo de recursos. – rmoh21

+0

@andih Estoy usando Jetty 8.1.1 – rmoh21

Respuesta

6

Las obras siguientes aparatos para mí:

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


} 

Recursos

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

y Clase Principal

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

¡Muchas gracias funcionó de maravilla! Solo hay un error en tu web.archivo xml - debe ser "ResteasyBootstrap" colocado fuera de "ResteasyBootstap" - falta la 'r'. – rmoh21

0

¿Está seguro de que @Path ("/ *") es la ruta correcta. Pruebe @Path ("/") quizás esto * sea un problema. Por lo que sé, las expresiones de ruta no aceptan expresiones regulares.

EDITAR

que estaba equivocada, puede utilizar expresiones regulares en @Path, al menos RESTEasy supports that.

+0

Probé que no sirve de nada. Aparece un 404 que no se encuentra en mi navegador cuando ingreso a localhost: 8080/ – rmoh21