加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 找不到类型为^的证据参数的隐含值

发布时间:2020-12-16 18:09:10 所属栏目:安全 来源:网络整理
导读:我正在尝试为Post请求编写测试 这是我的代码: val request = CreateLinkRequest(token = Some(validToken),billing_ref_id = Some("123"),store_id = Some("123"),agent_id = Some("123"))val endPoint = Uri(this.serverRootUrl + "path").toStringval pos
我正在尝试为Post请求编写测试

这是我的代码:

val request = CreateLinkRequest(token = Some(validToken),billing_ref_id = Some("123"),store_id = Some("123"),agent_id = Some("123"))

val endPoint = Uri(this.serverRootUrl + "path").toString
val post = Post(endPoint,request)
val pipeline = jsonAsStringPipeline(post)
val responseContents = Await.ready(pipeline,atMost = callMaxWaitDuration)

但这不编译,我不断收到此错误:

Error:(156,20) could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[CreateLinkSpec.this.CreateLinkRequest]
    val post = Post(endPoint,request)
               ^
Error:(156,20) not enough arguments for method apply: (implicit evidence$1: 

spray.httpx.marshalling.Marshaller[CreateLinkSpec.this.CreateLinkRequest])spray.http.HttpRequest in class RequestBuilder.
Unspecified value parameter evidence$1.
    val post = Post(endPoint,request)
                   ^

这是什么意思?

我该如何解决?

编辑:
这是身体中的json:

{ token:"123",billing_ref_id:"123",store_id:"123",agent_id:"123"}

这是代码中的对象:

private final case class CreateLinkRequest(
    token: Option[String] = Some(validToken),billing_ref_id: Option[String] = Some(Random.alphanumeric.take(10).mkString),agent_id: Option[String] = Some(Random.alphanumeric.take(3).mkString),store_id: Option[String] = Some(Random.alphanumeric.take(3).mkString)
    )

解决方法

您正在尝试调用Post方法,该方法将隐式Marshaller作为参数.请注意,只要编译器可以在范围中找到一个隐式参数,就不必提供隐式参数(有关implicits的更多信息,请查看此参数: Where does Scala look for implicits?)

但是,您的代码没有定义任何隐式Marshaller,因此编译器不知道如何将您的case类转换为HttpEntity.

在您的情况下,您希望将其转换为带有Content-Type的HttpEntity:application / json.要做到这一点,你只需要定义:implicit val CreateLinkRequestMarshaller:Marshaller [CreateLinkRequest].这告诉scala如何将您的case类转换为HttpEntity.

您还希望将它作为JSON传递给上下文,因此我们将定义我们的JsonProtocol,即MyJsonProtocol.

package test

import spray.http.HttpEntity
import spray.http._
import spray.json._
import spray.httpx.marshalling.{Marshaller,MarshallingContext}
import test.TestMarshaller.CreateLinkRequest


object MyJsonProtocol extends DefaultJsonProtocol {
  implicit def createLinkRequestFormat: JsonFormat[CreateLinkRequest] = jsonFormat4(CreateLinkRequest)
}

object TestMarshaller extends App {
  import MyJsonProtocol._

  case class CreateLinkRequest(token: Option[String],billing_ref_id: Option[String],store_id: Option[String],agent_id: Option[String])

  implicit val CreateLinkRequestMarshaller: Marshaller[CreateLinkRequest] = new Marshaller[CreateLinkRequest] {
    override def apply(value: CreateLinkRequest,ctx: MarshallingContext): Unit = {
      val entity = HttpEntity(MediaTypes.`application/json`,value.toJson.compactPrint)
      ctx.marshalTo(entity)
    }
  }

  // Here goes your test
}

请注意,您可以在其他位置定义这些含义,例如一个包,然后只需在测试类中导入它.这样会更好,因为你肯定想重用Marshaller.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读