【问题标题】:Mapping servlet in root在根目录中映射 servlet
【发布时间】:2013-04-07 09:49:32
【问题描述】:

这两者“共存”的正确配置是什么:

http://localhost:8888/index.html

http://localhost:8888/{some_path_value}

第一项将返回一个 html 页面,并且还包含将访问资源的 href,如 /public/images/bg.png 等。

现在第二个项目是一个 Restful api,它映射到同一个根上下文广告中,服务于页面(index.html、png、jpg、css、js 等)

所以我现在面临的问题是,当我像这样配置 Rest API servlet 映射时:

<servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Rest API 有效,但它有效地删除了对 index.html、css、js 等静态资源的访问,以呈现“主页”。

但是,如果我将映射更改为 /api/* 之类的东西,现在可以访问 GWT 应用程序,但 Rest API 的 PATH 不再是根目录。

那么我的应用有什么问题呢?我真的需要让两者在同一条路上共存。我最初的想法是做某种过滤器,但可能会有更简单、更合适的解决方案。

更新:

我的应用程序的guice 模块

public class MyModule implements Module
{
   public void configure(final Binder binder)
   {
      binder.bind(MyResource.class);
   }
}

web.xml

<context-param>
    <param-name>resteasy.guice.modules</param-name>
    <param-value>org.jboss.errai.ui.demo.server.MyModule</param-value>
</context-param>

<listener>
    <listener-class>
        org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
    </listener-class>
</listener>

<context-param>  
    <param-name>resteasy.servlet.mapping.prefix</param-name>  
    <param-value>/api</param-value>  
</context-param>

<servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

【问题讨论】:

    标签: java gwt servlets guice resteasy


    【解决方案1】:

    所以您的问题是您将所有根请求映射到其余 servlet,因为否则它不起作用。

    1. 如果有办法了解您的 REST servlet 的某些模式,您可以在 web.xml 中配置所有这些特定模式。但是url-pattern 使用了极其简单的语法,只允许使用whatever/**.extension,而且你的rest-servlet 似乎不符合这个要求。

    2. 另一种选择是使用像GuiceServletContextListener(由guice 提供)这样的高级servlet 调度程序并配置一个WebModule 巫婆丰富的正则表达式。修改您的 web.xml 以添加 WebModule 并配置该模块以处理 url 并使用适当的 servlet 进行调度(从您的 web.xml 中删除这些 servlet)

      <context-param>
        <param-name>resteasy.guice.modules&lt;/param-name>
        <param-value>org.jboss.errai.ui.demo.server.MyModule</param-value>
        <param-value>org.jboss.errai.ui.demo.server.MyWebModule</param-value>
      </context-param>
    
      public class MyWebModule extends ServletModule {
        @Override
        protected void configureServlets() {
          // Note: all servlets and filters must be singletons
          bind(FactoryServlet.class).in(Singleton.class);
          // Pass to the HttpServletDispatcher everything but urls ending with static extensions
          serveRegex(".+(?<!\\.(html|css|png|jpg))")
               .with(HttpServletDispatcher.class);
        }
      }    
    
    1. 最后一个选项是编写您自己的过滤器,您可以在其中检测路径是否与静态文件匹配并分派它。
      private FilterConfig config;
      public void init(FilterConfig filterConfig) throws ServletException {
        config = filterConfig;
      }
    
      public void doFilter(ServletRequest servletRequest,
          ServletResponse servletResponse, FilterChain filterChain)
          throws IOException, ServletException {
    
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
    
        File file = new File(config.getServletContext().getRealPath(req.getServletPath()));
        if (file.canRead()) {
          // NOTE: you have to set the most appropriate type per file
          resp.setContentType("text/html");
    
          // This depends on apache commons-io
          IOUtils.copy(new FileInputStream(file), resp.getOutputStream());
        } else {
          filterChain.doFilter(req, resp);
        }
      }
    

    【讨论】:

    • 感谢 Manolo 的这个想法,我实际上正在使用 Guice,也许我可以尝试一下
    • 当然,如果您对 reg-exp 有疑问,我可以帮忙。
    • 当然,我已经用 Guice 配置更新了我的问题
    • 我已经在使用 Guice 模块和我的 Resteasy 资源,我需要添加什么配置?
    • 什么是TwWebModule Rest 模块?
    猜你喜欢
    • 2012-11-04
    • 1970-01-01
    • 2015-03-01
    • 2011-11-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2012-01-20
    相关资源
    最近更新 更多