ID format issue as UUID String in Spring Boot with MongoDb
I created an example of Spring Boot with MongoDb. I have a problem in getting an entity by id with validation as it throws "
must be a valid UUID
".
Here is the entity shown below
When I call getAirportById(@PathVariable @Valid @UUID final String id) from Controller like localhost:8080/api/v1/airports/6781972fa25a3e577395c444 , I got this issue shown below
Here is the value stored in the collection
How can I fix the issue?61 Replies
⌛
This post has been reserved for your question.
Hey @direct_x_34! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
Maybe you need to format it differently?
for example
6781972f-a25a-3e577395c444
actually nvm I think that's too short
6781972fa25a3e577395c444
cannot be a valid UUID I think
like MongoDB ids are 96bit while UUIDs are 128bit@dan1st
I defined it here
@
And did you check whether the value stored in the DB/used as
_id
is actually the UUID you generated?It is stored as
_id : ObjectId(6781972fa25a3e577395c444)
as I mentioned before.Yeah and did
UUID.randomUUID().toString()
really return 6781972fa25a3e577395c444
for you?Exactly.
How did you check?
I couldn't solve it.
What?
UUID.randomUUID().toString() -> 6781972fa25a3e577395c444
All _ids seems like that
What tells you that MongoDB didn't just overwrite thatto a different value?
Where is the problem in entity
UUID.randomUUID().toString()
doesn't generate values like that - this is a value generated by MongoDB, not a UUID
ObjectId(...)
and UUID
and completely different thingsHow can I revise it in the entity?
First, in the entity
or whatever, the UUID generation is pointless
Secondly to fix the error: change the
@PathVariable @Valid @UUID final String id
to @PathVariable @Valid final String id
It should be UUID
it isn't
_id
cannot be a UUID
MongoDB doesn't allow that AFAIK
if you want a UUID, you'd need another fieldHow can I revise it?
revise what?
I use UUID for id as I said before.
you have two options:
- don't use UUIDs
- use UUIDs but not in
_id
(so you have an ObjectId
and a UUID
in different fields)
tertium non datorI want to use id as UUID
Why do you even have the idea that this would be possible?
well it might e possible if you don't use
ObjectId
How can I use id as UUID string format?
Is it possible to define it inside the entity?
?
I also asked ChatGpt but it couldn't provide the solutions
I already looked through the link before.
Can you try not using
String
for the ID?
but directly using UUID
?
like
and maybe also add spring.data.mongodb.uuid-representation=standard
to your application.propertiesIt didn't help me fix the issue
What happens with it?
Note that old documents won't work with that
so consider using a new database
I already delected all collections from mongodb
Did you insert a new object first?
Yeah
What does it look like in MongoDB?
How did you insert that object?
I did it
So your Java application inserted the object into the DB?
Can you print the content of the
id
field before inserting?
How do you get that JSON here?Its id is 6781a12a22e4f962650b7fec. Not UUID even if I defined
private String id = UUID.randomUUID().toString();
Can you show the code where you printed it in the Java application?
before saving it
and the code returning the response
id cannot perceive UUID as I said many times
I have no idea what you are talking about
I have no idea what you are talking about too.
I already mentioned the problem about not inserting UUID string format id but you said different things
You have some code that inserts the object in the DB
I asked you to include something printing the id field before that happens
and show that code
🎿 🐝 🦌
@direct_x_34 is it fixed now
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
No @ayylmao123xdd
can you show the validation for this uuid thing
where it gave you the 'must be a valid uuid'
but yea probably listen to daniel and read this article https://www.baeldung.com/java-mongodb-uuid
ah I also linked to that before
also they got an answer on SO and comments disagreeing with the answer (idk how trustworthy that comment is)
actually a spam detection system got alerted about that answer but I marked it as a false positive
well yea from what i saw the problem is that mongo doesnt hold ids that long
so he needs to make a custom key generation
shorter than uuid
that's what happens on SO: https://stackoverflow.com/a/79347990/10871900
Stack Overflow
ID format issue as UUID String in Spring Boot with MongoDb
I created an example of Spring Boot with MongoDb. I have a problem in getting an entity by id with validation as it throws "must be a valid UUID".
Here is the entity shown below
@Getter
@...
so
he just put the invalid uuid in controller
based on the comments
It should be e58ed763-928c-4155-bee9-fdbaaadc15f3 as I defined private String id = UUID.randomUUID().toString();
so just gotta put this in controller instead of the shorter string
🤦
:shrugging:
I'm not saying anything more
disaster
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.