我在使用Spray获取帖子正文时遇到问题:
当我发送Json电话时,我收到“请求内容格式错误:缺少实施”
我发送的Json是在请求中创建的Json(val测试) 正如代码中提到的,我从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 : https://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)
}
}
}
}
}
}
}
}
答案 0 :(得分:0)
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")
}