我想要一个接收一个item对象的服务,该对象包含;名称,描述,价格和图片。
答案 0 :(得分:1)
您可能要检查GitHub上lagom-recipes存储库中的file upload example。
基本上,这个想法是创建一个额外的Play路由器。之后,我们必须告诉Lagom按照reference documentation中的说明使用它(此功能自1.5.0起可用)。这是路由器的外观:
class FileUploadRouter(action: DefaultActionBuilder,
parser: PlayBodyParsers,
implicit val exCtx: ExecutionContext) {
private def fileHandler: FilePartHandler[File] = {
case FileInfo(partName, filename, contentType, _) =>
val tempFile = {
val f = new java.io.File("./target/file-upload-data/uploads", UUID.randomUUID().toString).getAbsoluteFile
f.getParentFile.mkdirs()
f
}
val sink: Sink[ByteString, Future[IOResult]] = FileIO.toPath(tempFile.toPath)
val acc: Accumulator[ByteString, IOResult] = Accumulator(sink)
acc.map {
case akka.stream.IOResult(_, _) =>
FilePart(partName, filename, contentType, tempFile)
}
}
val router = Router.from {
case POST(p"/api/files") =>
action(parser.multipartFormData(fileHandler)) { request =>
val files = request.body.files.map(_.ref.getAbsolutePath)
Results.Ok(files.mkString("Uploaded[", ", ", "]"))
}
}
}
然后,我们只是告诉Lagom使用它
override lazy val lagomServer =
serverFor[FileUploadService](wire[FileUploadServiceImpl])
.additionalRouter(wire[FileUploadRouter].router)
或者,我们可以使用PlayServiceCall
类。这是Lightbend团队的James Roper提供的有关如何执行此操作的简单草图:
// The type of the service call is NotUsed because we are handling it out of band
def myServiceCall: ServiceCall[NotUsed, Result] = PlayServiceCall { wrapCall =>
// Create a Play action to handle the request
EssentialAction { requestHeader =>
// Now we create the sink for where we want to stream the request to - eg it could
// go to a file, a database, some other service. The way Play gives you a request
// body is that you need to return a sink from EssentialAction, and when it gets
// that sink, it stream the request body into that sink.
val sink: Sink[ByteString, Future[Done]] = ...
// Play wraps sinks in an abstraction called accumulator, which makes it easy to
// work with the result of handling the sink. An accumulator is like a future, but
// but rather than just being a value that will be available in future, it is a
// value that will be available once you have passed a stream of data into it.
// We wrap the sink in an accumulator here.
val accumulator: Accumulator[ByteString, Done] = Accumulator.forSink(sink)
// Now we have an accumulator, but we need the accumulator to, when it's done,
// produce an HTTP response. Right now, it's just producing akka.Done (or whatever
// your sink materialized to). So we flatMap it, to handle the result.
accumulator.flatMap { done =>
// At this point we create the ServiceCall, the reason we do that here is it means
// we can access the result of the accumulator (in this example, it's just Done so
// not very interesting, but it could be something else).
val wrappedAction = wrapCall(ServiceCall { notUsed =>
// Here is where we can do any of the actual business logic, and generate the
// result that can be returned to Lagom to be serialized like normal
...
})
// Now we invoke the wrapped action, and run it with no body (since we've already
// handled the request body with our sink/accumulator.
wrappedAction(request).run()
}
}
}
通常来说,为此目的使用Lagom可能不是一个好主意。如PlayServiceCall
文档的GitHub问题所述:
我们回退到PlayServiceCall的许多用例与表示或特定于HTTP的使用(I18N,文件上载...)有关,它们指示:将lagom服务耦合到表示层,或将lagom服务耦合到显示层。运输。
(几年前)再次引用James Roper:
因此,目前,Lagom不支持multipart / form-data,至少不是开箱即用的。您可以下拉至较低级别的Play API来处理它,但是在Web网关中处理它可能会更好,在Web网关中,处理的所有文件都直接上传到存储服务(例如S3),然后Lagom服务可以存储与之关联的元数据。
您还可以在此处查看discussion,以提供更多的见解。