Java Restlet目录-未提供默认index.html文件

Java Restlet目录-未提供默认index.html文件,java,restlet,Java,Restlet,我有: 当按名称为目录中的所有文件提供服务时,此选项有效 但是,当您访问“/”时,它应该提供index.html,而不是。我尝试了所有的路径组合,额外的路由器等,但仍然不起作用 当您访问“/”时,您会得到一个200响应和一个应用程序/八位字节流内容类型。否则,响应为空。目录上的getIndexName向我保证它是index 我还尝试了getMetadataService().addExtension(“html”,MediaType.TEXT_html,true)以帮助它拾取index.html

我有:

当按名称为目录中的所有文件提供服务时,此选项有效

但是,当您访问“/”时,它应该提供
index.html
,而不是。我尝试了所有的路径组合,额外的路由器等,但仍然不起作用

当您访问“/”时,您会得到一个200响应和一个应用程序/八位字节流内容类型。否则,响应为空。目录上的
getIndexName
向我保证它是
index

我还尝试了
getMetadataService().addExtension(“html”,MediaType.TEXT_html,true)
以帮助它拾取index.html文件,但无效,并将请求中的accept标头设置为text/html

ETA:这与此处描述的问题相同(未解决):

有人能帮忙吗?它快把我逼疯了

经过一点修改后,我现在有了这个解决方案,但如果可能的话,我不想重定向:

Directory webdir = new Directory(getContext(), "clap://class/webapp");
webdir.setDeeplyAccessible(true);
router.attach("",webdir);

该行为是由
ClapClientHelper
类将目标标识为文件或目录的方式引起的。解决方案是用另一个几乎相同的类say
JarClapClientHelper
替换ClapClientHelper类。从
ClapClientHelper
复制源代码,并在
handleClassLoader
方法中更改以下代码段

        Redirector redirector = new Redirector(getContext(), "/index.html", Redirector.MODE_CLIENT_PERMANENT);
        TemplateRoute route = router.attach("/",redirector);
        route.setMatchingMode(Template.MODE_EQUALS);
现在,您需要加载此帮助程序,而不是默认的帮助程序。 (感谢@Thierry Boileau。)

有两种方法,一种不需要任何代码,另一种是编程。 第一个是让用户找到您的服务:

  • 将名为META-INF/services/org.restlet.engine.ClientHelper的文件添加到解决方案中
  • 这个文件应该只有一行文本:helper类的全名(包括包)
  • 第二个是以编程方式添加辅助对象:

      // The ClassLoader returns a directory listing in some cases.
      // As this listing is partial, it is of little value in the context
      // of the CLAP client, so we have to ignore them.
      if (url != null) {
        if (url.getProtocol().equals("file")) {
          File file = new File(url.getFile());
          modificationDate = new Date(file.lastModified());
    
          if (file.isDirectory()) {
            url = null;
          }
        //NEW CODE HERE
        } else if (url.getProtocol().equals("jar")) {
          try {
            JarURLConnection conn = (JarURLConnection) url.openConnection();
            modificationDate = new Date(conn.getJarEntry().getLastModifiedTime().toMillis());
            if (conn.getJarEntry().isDirectory()) {
              url = null;
            }
    
          } catch (IOException ioe) {
            getLogger().log(Level.WARNING,
                    "Unable to open the representation's input stream",
                    ioe);
            response.setStatus(Status.SERVER_ERROR_INTERNAL);
          }
        }
      }
    

    尝试使用WAR协议:

    Engine.getInstance().getRegisteredClients().add(0, new JarClapClientHelper(null));
    
    注意三个斜杠来标识war包根,否则您将得到
    NullPointerException


    (在我的例子中,我有一个Maven项目,我的索引文件放在生成的war包的根目录上)。

    您是否尝试过使用webdir.setIndexName(“index.html”)?
    Directory directory = new Directory(getContext(), "war:///");
    directory.setIndexName("index.html");
    router.attach("/", directory);