Clovis Warlop Clovis Warlop - 6 months ago 32
JSON Question

Scala (Spray/Akka) : The request content was malformed: an implementation is missing

I have a problem using Spray to get the post body:

When I send in a Json call I get "The request content was malformed: an implementation is missing"

The Json I send is the Json created in the request (val test)
As mentioned in the code I got the implementation from Providing a JsonFormat for a Sequence of Objects

CreateOrder.Scala

import spray.json._
import spray.httpx.unmarshalling._
import spray.httpx.marshalling._


case class CreateOrder (order_number: String,
order_source_code: String,
created_order_date: String,
shipping_address: String,
payment_method: String,
customer_name: String,
customer_tel: String,
customer_postcode: String,
customer_email: String,
ob_zone: String,
order_line_items: Seq[Order_Line_Items]
)
case class Order_Line_Items (order_line_item_number: String,
sku_number: String,
item_name: String,
paid_price: String
)

//Source : http://stackoverflow.com/questions/21756836/providing-a-jsonformat-for-a-sequence-of-objects
object CreateOrderProtocol extends DefaultJsonProtocol {
implicit object OrderItemsFormat extends RootJsonFormat[Order_Line_Items] {
def write(oli: Order_Line_Items) = JsObject(Map(
"order_line_item_number" -> JsString(oli.order_line_item_number),
"sku_number" -> JsNumber(oli.sku_number),
"item_name" -> JsString(oli.item_name),
"paid_price" -> JsString(oli.paid_price)
))
def read(value: JsValue): Order_Line_Items = ???
}
implicit object CreateOrderFormat extends RootJsonFormat[CreateOrder] {
def write(order: CreateOrder) = JsObject(Map(
"order_number" -> JsString(order.order_number),
"order_source_code" -> JsString(order.order_source_code),
"created_order_date" -> JsString(order.created_order_date),
"shipping_address" -> JsString(order.shipping_address),
"payment_method" -> JsString(order.payment_method),
"customer_name" -> JsString(order.customer_name),
"customer_postcode" -> JsString(order.customer_postcode),
"customer_email" -> JsString(order.customer_email),
"ob_zone" -> JsString(order.ob_zone),
"order_line_items" -> JsArray(order.order_line_items.map(_.toJson).toList)
))
def read(value: JsValue): CreateOrder = ???
}
}


Main.Scala

path("dev" / "order" / "create") {
headerValueByName("AppId") { appId =>
headerValueByName("AppSecret") { appSecret =>
post {
entity(as[CreateOrder]){ req =>
respondWithMediaType(`application/json`) {
complete{
val test = new CreateOrder("1","2","3","4","5","6","7","8","9","10",Seq( new Order_Line_Items("11","12","13","14"), new Order_Line_Items("21","22","23","24") ) )
if(elasticCaller.api_check(appId,appSecret)){
val reply = Map("Status"->"Success")
ModelConverter.fromAnyToJson(test)
}else{
val reply = Map("Status"->"Failed")
ModelConverter.fromAnyToJson(test)
}
}
}
}
}
}
}
}

Fon Fon
Answer
object CreateOrderProtocol extends DefaultJsonProtocol with SprayJsonSupport {

  implicit val OrderLineItemsFormat = jsonFormat(Order_Line_Items, "order_line_item_number", "sku_number", "item_name", "paid_price")
  implicit val OrderItemsFormat = jsonFormat(CreateOrder, "order_number", "order_source_code", "created_order_date",
    "shipping_address", "payment_method", "customer_name", "customer_tel", "customer_postcode", "customer_email",
    "ob_zone", "order_line_items")

}

from: Spray-json deserializing nested object

Comments