Reputation: 107
I'm trying to use Lift to do a Rest service, put I'm having problem when I try to pass a json body.
Model:
case class TestData(substore:String) {}
object TestData {
private implicit val formats = net.liftweb.json.DefaultFormats
implicit def toJson(item: TestData): JValue = Extraction.decompose(item)
def apply(in: JValue): Box[TestData] = Helpers.tryo{in.extract[TestData]}
def unapply(in: Any): Option[TestData] = in match {
case i: TestData => Some(TestData(i.substore)
case _ => None
}
}
Rest:
object RestExample extends RestHelper {
serve {
case "api" :: "user" :: AsLong(id) :: Nil JsonGet _ => JInt(id)
case "api" :: "test2" :: Nil JsonPut _ => TestData("testeee"): JValue
case "api" :: "test" :: Nil JsonPut TestData(testData) -> _ => testData : JValue
}
}
The simple put test works:
curl -i -H "Content-Type: application/json" -X PUT http://localhost:8080/api/test2
But i can't get the test with a json body working:
curl -i -H "Content-Type: application/json" -X PUT -d '{"substore":"abs"}' http://localhost:8080/api/test
in this case I always receive:
<html> <body>The Requested URL /api/test was not found on this server</body> </html>
Upvotes: 1
Views: 174
Reputation: 10236
seems like a pattern-matching problem.
Would replacing TestData(testData)
with JObject(JField("substore", JString(substore)))
solve your problem? You'll have substore: String
in the scope with such pattern matching.
Also, in the TestData
object, why do you use Any
instead of JValue
? And where would it's apply
method called, I don't see any usages (TestData
isn't called with a JValue
anywhere).
Upvotes: 1