json - 在 tomcat Web 服务器中托管 apple-app-association-file

标签 json maven spring-mvc tomcat servlets

我们需要在我们的应用程序中启用通用链接。我们有一个基于 Java 的 Web 应用程序(spring)和一个 iOS 应用程序。要按照苹果启用通用链接,我们需要创建一个 json 文件 apple-app-association-file 并将此文件托管在服务器中。 现在java web应用程序部署在windows服务器的tomcat中,apche 2.4被用作web服务器。请让我知道如何在 tomcat 或 Web 服务器或 war 文件中(在代码中)托管 apple-app-association-file,我们使用的是 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 请求。即使我这样做,执行 servet 只是为了访问文件也不是一个好主意

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

最佳答案

首先将文件重命名为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

You can place the file at the root of your server or in the .well-known subdirectory.

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

关于json - 在 tomcat Web 服务器中托管 apple-app-association-file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48093807/

相关文章:

rest - 使用 spring security 的 Angular 2 登录

java - 如何使用 Spring-MVC 创建 "Update"/"Edit"的 SQL 语法?

java - JSON 序列化时 jsonrpc4j null

json - 如何使用 lodash 将字符串连接到现有数组元素

javascript - Opera 中的 JSON 解析抛出 `Illegal number format (trailing decimal dot)`

javascript - 无法使用jquery调用json结果

java - 升级到 Jetty 9 时 FakeHttpServer 崩溃

JavaFX FXMLLoader InspirationTargetException 和 NullpointerException

java - Springframework 无法解析 Controller 上的配置类

java - opennlp 安装 - 没有找到适合火车的方法