[英]Spring SCALA return list in RestController
How can I return a SCALA list or sequence in Sprint @RestController. 如何在Sprint @RestController中返回SCALA 列表或序列。 List return value is not being serialized properly.
列表返回值未正确序列化 。
The result is: 结果是:
[GET] http://localhost:9090/devices
{"empty":false,"traversableAgain":true}
Do I need to import Jackson ObjectMapper com.fasterxml.jackson
for proper REST get result serialization on a list? 我是否需要导入Jackson ObjectMapper
com.fasterxml.jackson
以在列表中进行正确的REST get result序列化?
My RestController looks like this: 我的RestController看起来像这样:
@RestController
class DeviceController {
var devices = Set[Device]()
@RequestMapping(value = Array("/devices"), method = Array(RequestMethod.GET))
def accounts() : List[Device] = devices.toList
}
Spring was NOT designed with SCALA in mind - hence it cannot handle SCALA lists properly. Spring 没有考虑到SCALA的设计 - 因此它无法正确处理SCALA列表。 Nor can it handle Seq[Device].
它也不能处理Seq [Device]。
Just use SCALA's JavaConvertes package to easliy convert SCALA list to JAVA list. 只需使用SCALA的JavaConvertes包即可轻松将SCALA列表转换为JAVA列表。
import scala.collection.JavaConverters._
@RestController
class DeviceController {
var devices = Set[Device]()
@RequestMapping(value = Array("/devices"), method = Array(RequestMethod.GET))
def accounts() : java.util.List[Device] = {
devices.toList.asJava
}
}
and the result will be: 结果将是:
[GET] http://localhost:9090/devices
[{"name":"first device"},{"name":"second device"}]
Remember to change the result type to: java.util.List[Device]
请记住将结果类型更改为:
java.util.List[Device]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.