【问题标题】:Scala case class to json schemaScala案例类到json模式
【发布时间】:2018-08-22 00:01:34
【问题描述】:

我想生成案例类的 json 模式,以便向其他服务提供一些信息,这些服务将使用 rest api 公开我的应用程序

我有这门课:

case class Vendor(
                  name: String,
                  synonyms: List[String],
                  transalit: String,
                  urlPart: String)

我怎样才能生成这样的:

{
   "type":"object",
   "properties":{
      "name":{
         "type":"string"
      },
      "synonyms":{
         "type":"array",
         "items":{
            "type":"string"
         }
      },
      "translit":{
         "type":"string"
      },
      "urlPart":{
         "type":"string"
      }
   }
}

我找到了这个:https://github.com/coursera/autoschema 但 sbt 找不到依赖项。

我也发现了这个Is there a way to get a JSON-Schema from a Scala Case Class hierarchy?,这个问题与我的非常相似,但没有答案..

也许我正在寻找不存在的答案。使用其他技术可能会更好

【问题讨论】:

    标签: json scala rest


    【解决方案1】:

    似乎自动模式的 Maven 工件不存在,这就是 sbt 找不到依赖项的原因。

    好消息是,使用 sbt,您可以从 github 导入项目并将其添加为依赖项。在您的 build.sbt 中添加以下内容:

    lazy val autoschemaProject =
      ProjectRef(uri("https://github.com/coursera/autoschema.git"), "autoschema")
    
    lazy val root = (project in file(".")).dependsOn(autoschemaProject)
    

    注意root 可能已经在您的build.sbt 中定义,在这种情况下只需添加dependsOn(autoschemaProject)
    我使用 sbt 0.13.7 对此进行了测试,并设法使用自动模式从案例类生成 json 模式。

    【讨论】:

    • 我得到这个依赖错误:[warn] 注意:未解析的依赖路径:[warn] com.typesafe.play:play-json_2.10:2.3.2 (/home/user256/.sbt/ 0.13/staging/00472dcce208f679d89f/autoschema/build.sbt#L23-27) [warn] +- org.coursera:autoschema_2.10:0.1 [trace] 堆栈跟踪被抑制:最后运行 {github.com/coursera/autoschema.git}autoschema/*:update for完整的输出。 [错误] ({github.com/coursera/autoschema.git}autoschema/*:update) sbt.ResolveException: unresolved dependency: com.typesafe.play#play-json_2.10;2.3.2: not found
    • 奇怪,我没有得到那个错误。也许看看this stackoverflow 问题。请注意,您可能必须将解析器添加到 autoschema build.sbt: (/home/user256/.sbt/0.13/staging/00472dcce208f679d89f/autoschema/build.sbt)。
    • 在我的项目中,我也使用 play-json:“com.typesafe.play”%%“play-json”%“2.4.0”。我想当我制作使用较低版本的 play-json 库的dependsOn 项目时,sbt 会感到困惑。稍后我会尝试将解析器添加到 auto schema/build.sbt 并让您知道。谢谢
    • 您必须为两个项目设置相同的版本,否则您将遇到冲突。但是,此版本差异不应影响依赖项解析。
    【解决方案2】:

    项目已分叉: https://github.com/sauldhernandez/autoschema

    现在,您可以通过将此依赖项添加到您的 build.sbt 来获取工件:

    libraryDependencies += "com.sauldhernandez" %% "autoschema" % "1.0.4"
    

    【讨论】:

      猜你喜欢
      • 2013-04-13
      • 2017-02-25
      • 2017-04-25
      • 2013-08-04
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多