【问题标题】:Host apple-app-association-file in tomcat web server在 tomcat Web 服务器中托管 apple-app-association-file
【发布时间】:2018-06-14 02:20:24
【问题描述】:

我们需要在我们的应用程序中启用通用链接。我们有一个基于 java 的 web 应用程序(spring)和一个 iOS 应用程序。要根据苹果启用通用链接,我们需要创建一个 json 文件 apple-app-association-file 并将此文件托管在服务器中。 现在 java web 应用程序部署在 windows 服务器的 tomcat 中,并且 apche 2.4 被用作 web 服务器。请让我知道如何将 apple-app-association-file 托管在 tomcat 或 web 服务器或 war 文件中(在代码内部),我们使用的是 maven 结构。 根据文档,我们需要删除文件扩展名,文件应该如下访问:

网页应用网址:https://xyz.example.com

其中 xyz.example.com 映射到 web 应用程序,该应用程序位于 tomcat 的 webapp 文件夹中。(localhost:8080/webApp)

apple-app-association-file 被访问为:https://xyz.example.com/apple-app-association-file

现在由于扩展不存在,我该如何托管它。我是否需要更改代码并将其视为 servle 请求。即使我这样做了,为了访问文件而执行服务也不是一个好主意

此外,使用正确的 MIME 类型提供文件也很重要,对于通用链接,它可以作为 application/json 提供。如何在 tomcat 或 java web app(spring) 中设置 mime 类型

【问题讨论】:

  • 有人可以帮我卡住

标签: json maven spring-mvc tomcat servlets


【解决方案1】:

首先将文件重命名为apple-app-site-association.json,然后编写下一个Spring配置:

@EnableWebMvc
public class WebClientConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/.well-known/*")
      .addResourceLocations("/path/to/your/static/resources")
      .resourceChain(true)
      .addResolver(new PathResourceResolver() {
        @Override
        protected Resource getResource(String resourcePath, Resource location) throws IOException {
          if (resourcePath.equals("apple-app-site-association")) {
            return location.createRelative("apple-app-site-association.json");
          }
          return super.getResource(resourcePath, location);
        }
      });

  }
}

如此处所述:developer.apple.com

您可以将文件放在服务器的根目录或 .well-known 子目录中。

然后该文件将使用正确的 MIME 类型“application/json”提供并访问为:https://xyz.example.com/.well-known/apple-app-association-file

【讨论】:

    【解决方案2】:

    pITer Simonov 的解决方案对我有用!但我必须添加根路径 里面

    < servlet-mapping > (in web.xml)

    像这样:

    < url-pattern >/</url-pattern >
    

    之后,资源处理程序就可以正常工作了!

    【讨论】:

      【解决方案3】:

      我用标准的 REST 控制器 + 端点做到了。

      import lombok.extern.slf4j.Slf4j;
      import org.springframework.http.MediaType;
      import org.springframework.util.StreamUtils;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      import java.io.IOException;
      import java.io.InputStream;
      import java.nio.charset.Charset;
      
      @RestController
      @RequestMapping("/.well-known")
      @Slf4j
      public class WebClientConfig {
      
          @GetMapping(value = "/apple-app-site-association",
              produces = MediaType.APPLICATION_JSON_VALUE)
          public String addResourceHandlers() {
              String json = "";
              InputStream inputStream = getClass().getResourceAsStream("/apple-app-association.json");
              try(InputStream stream = inputStream) {
                  json = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
              } catch (IOException ioe) {
                  log.error("Apple app association could not be retrieved! iOS app will be impacted. Error: " +
                      ioe.getMessage());
              }
              return json;
          }
      }
      

      注意:apple-app-asociation.json 文件位于 src/main/resources 下

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        • 2017-07-11
        • 1970-01-01
        • 1970-01-01
        • 2020-03-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多