2015-01-19 7 views
1

이 문제를 해결하기 위해 serializer/deserializer를 작성해야하지만 정확하게는 알 수 없습니다. 리프트 웹 프레임 워크를 사용하는 몇 가지 예가 있지만 그 중 하나가없는 것은 아닙니다. 이 작업을 수행하는 방법에 대한 도움이 필요하십니까? RegistrationsRoutes 클래스마샬링 중에 org.bson.types.ObjectId를 String으로 변환하는 방법은 무엇입니까?

val route: Route = { 
    withService("registrations") { service => 

    import model.RegistrationJsonProtocol._ 

    postRegistration { 
     entity(as[Registration]) { registration => 
     val future = (service ? PostRegistrationMessage(registration)).mapTo[Registration] 

     onComplete(future) { 
      case Success(result) => 
      log.debug(s"result: ${result}") 
      complete(result) 

      case Failure(e) => 
      log.error(s"Error: ${e.toString}") 
      complete(StatusCodes.InternalServerError, Message(ApiMessages.UnknownException)) 
     }   
     } 
    } 
    } 
} 

에서

등록 모델

package model 

import spray.json._ 
import spray.json.DefaultJsonProtocol._ 
import spray.httpx.SprayJsonSupport 
import com.mongodb.casbah.Imports._ 
import org.bson.types.ObjectId 
import com.mongodb.DBObject 
import com.mongodb.casbah.commons.{MongoDBList, MongoDBObject} 

case class Registration(
    system: String, 
    identity: String, 
    id: Option[String] = None) 

object RegistrationJsonProtocol extends DefaultJsonProtocol { 
    implicit val adsRegistrationFormat = jsonFormat3(Registration) 
} 

object RegistrationMap { 
    def toBson(registration: Registration): DBObject = { 
    MongoDBObject(
     "system"   -> registration.system, 
     "identity"  -> registration.identity, 
     "_id"   -> new ObjectId(registration.id.getOrElse(new ObjectId().toString)) 
    ) 
    } 

    def fromBson(o: DBObject): Registration = { 
    Registration(
     system = o.as[String]("system"), 
     identity = o.as[String]("identity"), 
     id = Some(o.as[String]("_id")) 
    ) 
    } 
} 

코드는 얻기 완료 처리 할 때

13:08:17.538 [microservice-system-akka.actor.default-dispatcher-5] DEBUG akka.actor.ActorSystemImpl - result: Registration(system1,identity1,Some(54bd48110364eb78f7b84ce3)) 
13:08:17.643 [microservice-system-akka.actor.default-dispatcher-5] ERROR akka.actor.RepointableActorRef - Error during processing of request HttpRequest(POST,http://localhost:8878/api/v1/registrations,List(Accept-Language: en-US, en;q=0.8, es;q=0.6, te;q=0.4, Accept-Encoding: gzip, deflate, DNT: 1, Content-Type: application/json, Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo, User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36, Content-Length: 165, Connection: keep-alive, Host: localhost:8878),HttpEntity(application/json,{"system":"system1", "identity":"identity1"),HTTP/1.1) 
java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to java.lang.String 
    at spray.json.BasicFormats$StringJsonFormat$.write(BasicFormats.scala:119) ~[spray-json_2.10-1.2.6.jar:na] 
    at spray.json.PimpedAny.toJson(package.scala:39) ~[spray-json_2.10-1.2.6.jar:na] 
    at spray.json.StandardFormats$OptionFormat.write(StandardFormats.scala:34) ~[spray-json_2.10-1.2.6.jar:na] 
    at spray.json.StandardFormats$OptionFormat.write(StandardFormats.scala:32) ~[spray-json_2.10-1.2.6.jar:na] 
    at spray.json.ProductFormats$class.productElement2Field(ProductFormats.scala:36) ~[spray-json_2.10-1.2.6.jar:na] 
    at com.ericsson.admcore.model.ADSRegistrationJsonProtocol$.productElement2Field(ADSRegistration.scala:20) ~[classes/:na] 
    at spray.json.ProductFormatsInstances$$anon$6.write(ProductFormatsInstances.scala:155) ~[spray-json_2.10-1.2.6.jar:na] 
    at spray.json.ProductFormatsInstances$$anon$6.write(ProductFormatsInstances.scala:146) ~[spray-json_2.10-1.2.6.jar:na] 
    at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonMarshaller$1.apply(SprayJsonSupport.scala:43) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonMarshaller$1.apply(SprayJsonSupport.scala:42) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.Marshaller$MarshallerDelegation$$anonfun$apply$1.apply(Marshaller.scala:58) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.Marshaller$MarshallerDelegation$$anonfun$apply$1.apply(Marshaller.scala:58) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.Marshaller$MarshallerDelegation$$anonfun$apply$2.apply(Marshaller.scala:61) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.Marshaller$MarshallerDelegation$$anonfun$apply$2.apply(Marshaller.scala:60) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.Marshaller$$anon$2.apply(Marshaller.scala:47) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.BasicToResponseMarshallers$$anon$1.apply(BasicToResponseMarshallers.scala:35) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.BasicToResponseMarshallers$$anon$1.apply(BasicToResponseMarshallers.scala:22) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.ToResponseMarshaller$$anonfun$compose$1.apply(Marshaller.scala:69) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.ToResponseMarshaller$$anonfun$compose$1.apply(Marshaller.scala:69) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.ToResponseMarshaller$$anon$3.apply(Marshaller.scala:81) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.ToResponseMarshallable$$anon$6.marshal(Marshaller.scala:141) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.ToResponseMarshallable$$anon$7.apply(Marshaller.scala:145) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.httpx.marshalling.ToResponseMarshallable$$anon$7.apply(Marshaller.scala:144) ~[spray-httpx_2.10-1.3.1.jar:na] 
    at spray.routing.RequestContext.complete(RequestContext.scala:237) ~[spray-routing_2.10-1.3.1.jar:na] 
    at spray.routing.directives.RouteDirectives$$anonfun$complete$1$$anon$3.apply(RouteDirectives.scala:49) ~[spray-routing_2.10-1.3.1.jar:na] 
    at spray.routing.directives.RouteDirectives$$anonfun$complete$1$$anon$3.apply(RouteDirectives.scala:48) ~[spray-routing_2.10-1.3.1.jar:na] 
    at spray.routing.directives.OnCompleteFutureMagnet$$anon$1$$anonfun$happly$1$$anonfun$apply$1.apply(FutureDirectives.scala:63) ~[spray-routing_2.10-1.3.1.jar:na] 
    at spray.routing.directives.OnCompleteFutureMagnet$$anon$1$$anonfun$happly$1$$anonfun$apply$1.apply(FutureDirectives.scala:62) ~[spray-routing_2.10-1.3.1.jar:na] 
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) ~[scala-library.jar:na] 
    at scala.concurrent.impl.ExecutionContextImpl$$anon$3.exec(ExecutionContextImpl.scala:107) ~[scala-library.jar:na] 
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library.jar:na] 
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library.jar:na] 
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library.jar:na] 
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library.jar:na] 

답변

1

내가이를 테스트 할 수있다 (결과) 문 오류를 다음 있지만, 나는이 문제가 fromBson 방법의 다음 줄에 있다고 생각한다 (나는이 메소드가 호출되는 방식) 사용자 코드에서 볼 수 있지만 :

id = Some(o.as[ObjectId]("_id").toString) 

id = Some(o.as[String]("_id")) 

변경이 직렬화 오류가 사라해야합니다.

0

예, 아래와 같이 toString 시도 :

ObjectId.toString을() ObjectId가의 문자열 표현을 반환합니다(). 이 문자열 값의 형식은 ObjectId (...)입니다.

실시 예 다음의 예는 ObjectId가()에 toString() 메서드 호출.

ObjectId가 ("507c7f79bcf86cd7994f6c0e")를 toString()

REF : https://docs.mongodb.com/manual/reference/method/ObjectId.toString/