将大POJO转换为case类。

时间:2022-03-13 15:33:36

Now I'm migrating legacy Java project to Scala.

现在我将遗留Java项目迁移到Scala。

I faced the following problem: I call remote service that returns java.util.List<SomePojo>. SomePojo class contains about 50 fields and I'm interested what are the best practices to pass it to UI in JSON format.

我遇到了以下问题:我调用返回java.util.List 的远程服务。SomePojo类包含大约50个字段,我对以JSON格式传递给UI的最佳实践感兴趣。

I'm using scalatra framework and it is just fine, but how to put this POJO to case class while case classes are limited with 21 fields? Or is there better way?

我使用的是scalatra框架,它很好,但是如何将POJO放到case类中,而case类只有21个字段?还是有更好的方法?

1 个解决方案

#1


1  

This was fixed in Scala 2.11.

这在Scala 2.11中得到了修正。

If you can't use 2.11, split up the fields into separate case classes, and then combine those in an aggregate one.

如果不能使用2.11,将字段拆分为单独的case类,然后将它们组合在一起。

case class Part1(a: Int, b: Int, c: Int)
case class Part2(d: Int, e: Int, f: Int)
case class Aggregate(part1: Part1, part2: Part2)

val aggregate = Aggregate(Part1(0, 1, 2), Part2(3, 4, 5))

aggregate match {
    case Aggregate(Part1(a, b, c), Part2(d, e, f)) =>
}

#1


1  

This was fixed in Scala 2.11.

这在Scala 2.11中得到了修正。

If you can't use 2.11, split up the fields into separate case classes, and then combine those in an aggregate one.

如果不能使用2.11,将字段拆分为单独的case类,然后将它们组合在一起。

case class Part1(a: Int, b: Int, c: Int)
case class Part2(d: Int, e: Int, f: Int)
case class Aggregate(part1: Part1, part2: Part2)

val aggregate = Aggregate(Part1(0, 1, 2), Part2(3, 4, 5))

aggregate match {
    case Aggregate(Part1(a, b, c), Part2(d, e, f)) =>
}