Scala Spray Templated Custom Routing Directive

时间:2016-06-01 23:17:05

标签: json scala generics spray shapeless

好的......我有这个:

  def convertPost  = extract {
    _.request.entity.asString.parseJson.convertTo[CustomClass]
  }

  private def myRoute: Route =
    (post & terminalPath("routeness")) {
      convertPost { req =>
        detach() {
           ThingHandler.getMyResults( req )
        }
      }
    }

但我想模仿它,就像这样:

  def convertPost[T] = extract {
    _.request.entity.asString.parseJson.convertTo[T]
  }

  private def myRoute: Route =
    (post & terminalPath("routeness")) {
      convertPost[CustomClass] { req =>
        detach() {
           ThingHandler.getMyResults( req )
        }
      }
    }

但这不起作用。我正在使用spray-json-shapeless。我的错误是

Error:(28, 50) Cannot find JsonReader or JsonFormat type class for T
    _.request.entity.asString.parseJson.convertTo[T]
                                                 ^

当我尝试:

  def getStuff[T] = extract {
    _.request.entity.asInstanceOf[T] // .convertTo[T]
  }

它给出了:

spray.http.HttpEntity$NonEmpty cannot be cast to com.stuff.CustomClass

0 个答案:

没有答案