0

I am currently working on a kotlin multi project solution. I have one project defining some data classes and defining an api to access a mongodb. The objectId is created automatically. This project is using morphia:1.3.2. Entries are stored using this function:

fun store(myClass: MyClass) = db.save(myClass).let { myClass.id?.toHexString() ?: "0" }

Now I'm using this project in a spring-boot kotlin project. I created a small web page with some filters. These filters should be applied on my query. So far so good, everything is working.

The results of my query are returned via my Rest-controller without any conversions. In my web page I want to print the ObjectId foreach result.

But the ObjectId is not a String as it used to be, it is an object.

id:
  counter:15304909
  date:"2018-08-27T23:45:35.000+0000"
  machineIdentifier:123456
  processIdentifier:1234
  time:1535413535000
  timeSecond:1535413535
  timestamp:1535413535

Is it possible to force morphia to return the objectId in the String representation? Or is there a on Option to activate the correct mapping? Or do I have to touch each result one by one and convert the object id to the hexadecimal string representation? I hope that there is a better, and quicker solution then this.

I am also not able to remap the object to a valid id, due to an java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 exception. The request looks like this:

myClass?id={"timestamp":1535413631,"machineIdentifier":123456,"processIdentifier":1234,"counter":16576969,"time":1535413631000,"date":"2018-08-27T23:47:11.000+0000","timeSecond":1535413631}    

I'm a little bit out of ideas, how to fix this issue.

Schere
  • 35
  • 5

1 Answers1

0

Depending on your REST framework, you would need to provided a serializer for writing out that ObjectId as its String version, say. Most such frameworks make that transparent once it's configured so you need only worry about returning your objects out of your REST service and the framework will serialize properly.

I, personally, wouldn't muck about by trying to change how it's serialized in the database. ObjectId is a pretty good _id type and I wouldn't change it.

evanchooly
  • 6,102
  • 1
  • 16
  • 23
  • 1
    With the right keywords it was an easy task. This might help someone one day: [link](https://stackoverflow.com/questions/36494953/how-to-serialize-nested-objectid-to-string-with-jackson) – Schere Aug 28 '18 at 15:53