简体   繁体   English

Scala案例类到JSON模式

[英]Scala case class to json schema

I want to generate json schema of case class in order to provide some information to other service that is going to expose my app with rest api 我想生成case类的json模式,以便向其他服务提供一些信息,这些服务将使用rest api公开我的应用

I've got this class: 我有这节课:

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

How can i generate like this: 我如何生成这样的:

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

i found this: https://github.com/coursera/autoschema but sbt can't find dependency. 我发现了这一点: https : //github.com/coursera/autoschema但sbt找不到依赖项。

also i found this Is there a way to get a JSON-Schema from a Scala Case Class hierarchy? 我也发现这有没有办法从Scala Case Class层次结构中获取JSON-Schema? and this question is very similar to mine but there is no answer.. 这个问题和我的非常相似,但是没有答案。

May be i'm looking for answer that doesn't exist. 可能是我在寻找不存在的答案。 May be it's better to use some other techniques 也许使用其他技巧更好

It seems that the Maven artifact for autoschema does not exist and this is why sbt can't find the dependency. 看来,用于自动模式的Maven工件不存在,这就是为什么sbt找不到依赖项的原因。

Good news is that with sbt you can import a project from github and add it as a dependency. 好消息是,使用sbt可以从github导入项目并将其添加为依赖项。 In your build.sbt add the following: 在您的build.sbt添加以下内容:

lazy val autoschemaProject =
  ProjectRef(uri("https://github.com/coursera/autoschema.git"), "autoschema")

lazy val root = (project in file(".")).dependsOn(autoschemaProject)

Notice that root might already be defined in your build.sbt , in this case only add dependsOn(autoschemaProject) . 注意, root可能已经在您的build.sbt定义了,在这种情况下,只添加dependsOn(autoschemaProject)
I tested this with sbt 0.13.7 and I managed to use autoschema to generate a json schema from a case class. 我使用sbt 0.13.7对此进行了测试,并设法使用自动模式从案例类生成json模式。

The project has been forked: https://github.com/sauldhernandez/autoschema 该项目已经分叉: https://github.com/sauldhernandez/autoschema : https://github.com/sauldhernandez/autoschema

Now, you can get the artifact by adding this dependency to your build.sbt: 现在,可以通过将以下依赖项添加到build.sbt中来获得工件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM