9

Jetty と RESTEasy を統合する方法に関するリンクはありますか? RESTEasy と Jetty を一緒に構成しようとしてちょっと行き詰まっています....そして、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から直接コピーされます。

リソース ファイル内のリンクを開こうとすると、返されるエラーは HTTP 404 です。表面上はすべて合理的に見えますが、何か提案はありますか?

私のリソースファイルは次のようになります:

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 が起動したときに表示されるプリントです。

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:開始 oejwWebAppContext{/,file:/C:/Users/xyz/Anotherproj1/src/webapp} 2012-04 -10 09:54:27.319:INFO:oejsh.ContextHandler:開始 oejwWebAppContext{/,file:/C:/Users/xyz/Anotherproj1/src/webapp} 2012-04-10 09:54:27.381:INFO:oejs。 AbstractConnector:開始 SelectChannelConnector@0.0.0.0:8080

4

3 に答える 3

6

以下は私のために働きます:

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

}
于 2012-04-14T04:03:35.253 に答える
0

@Path("/*") が正しいパスであると確信していますか。@Path("/") を試してみてください * おそらくこれが問題です。私の知る限り、パス式は正規表現を受け入れません。

編集

私は間違っていました。@Path で正規表現を使用できます。少なくともRESTEasy はそれをサポートしています。

于 2012-04-12T21:36:19.213 に答える
0

web.xmlなしでRESTEasy と Jetty を連携させるには、pom.xml で resteasy-servlet-initializer に依存していることを確認してください。

これが役立つ場合があります (JBoss RESTEasy ドキュメント): https://docs.jboss.org/resteasy/docs/3.0.4.Final/userguide/html/Installation_Configuration.html#d4e111

于 2016-03-26T20:14:35.740 に答える