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
@Setter
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "airport-collection")
public class AirportEntity extends BaseEntity {

@Id
@Indexed(unique = true)
@Field(name = "_id")
private String id = UUID.randomUUID().toString();

@Field(name = "AIRPORT_NAME")
private String name;

@Field(name = "CITY_NAME")
private String cityName;

}
@Getter
@Setter
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "airport-collection")
public class AirportEntity extends BaseEntity {

@Id
@Indexed(unique = true)
@Field(name = "_id")
private String id = UUID.randomUUID().toString();

@Field(name = "AIRPORT_NAME")
private String name;

@Field(name = "CITY_NAME")
private String cityName;

}
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
{
"time": "2025-01-11T00:55:27.5670908",
"httpStatus": "BAD_REQUEST",
"header": "VALIDATION ERROR",
"message": "Constraint violation",
"isSuccess": false,
"subErrors": [
{
"message": "must be a valid UUID",
"field": "id",
"value": "6781972fa25a3e577395c444",
"type": "String"
}
]
}
{
"time": "2025-01-11T00:55:27.5670908",
"httpStatus": "BAD_REQUEST",
"header": "VALIDATION ERROR",
"message": "Constraint violation",
"isSuccess": false,
"subErrors": [
{
"message": "must be a valid UUID",
"field": "id",
"value": "6781972fa25a3e577395c444",
"type": "String"
}
]
}
Here is the value stored in the collection
_id : ObjectId(6781972fa25a3e577395c444)
AIRPORT_NAME : String
CITY_NAME : String
_class : Entity clas
_id : ObjectId(6781972fa25a3e577395c444)
AIRPORT_NAME : String
CITY_NAME : String
_class : Entity clas
How can I fix the issue?
61 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @direct_x_34! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st3w ago
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
direct_x_34
direct_x_34OP3w ago
@dan1st
@Id
@Indexed(unique = true)
@Field(name = "_id")
private String id = UUID.randomUUID().toString();
@Id
@Indexed(unique = true)
@Field(name = "_id")
private String id = UUID.randomUUID().toString();
I defined it here
dan1st
dan1st3w ago
@ And did you check whether the value stored in the DB/used as _id is actually the UUID you generated?
direct_x_34
direct_x_34OP3w ago
It is stored as _id : ObjectId(6781972fa25a3e577395c444) as I mentioned before.
dan1st
dan1st3w ago
Yeah and did UUID.randomUUID().toString() really return 6781972fa25a3e577395c444 for you?
direct_x_34
direct_x_34OP3w ago
Exactly.
dan1st
dan1st3w ago
How did you check?
direct_x_34
direct_x_34OP3w ago
I couldn't solve it.
dan1st
dan1st3w ago
What?
direct_x_34
direct_x_34OP3w ago
UUID.randomUUID().toString() -> 6781972fa25a3e577395c444
direct_x_34
direct_x_34OP3w ago
No description
direct_x_34
direct_x_34OP3w ago
All _ids seems like that
dan1st
dan1st3w ago
What tells you that MongoDB didn't just overwrite thatto a different value?
direct_x_34
direct_x_34OP3w ago
Where is the problem in entity
dan1st
dan1st3w ago
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 things
direct_x_34
direct_x_34OP3w ago
How can I revise it in the entity?
dan1st
dan1st3w ago
First, in the entity
@Id
@Indexed(unique = true)
@Field(name = "_id")
private String id = "";
@Id
@Indexed(unique = true)
@Field(name = "_id")
private String id = "";
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
direct_x_34
direct_x_34OP3w ago
It should be UUID
dan1st
dan1st3w ago
it isn't _id cannot be a UUID MongoDB doesn't allow that AFAIK if you want a UUID, you'd need another field
direct_x_34
direct_x_34OP3w ago
How can I revise it?
dan1st
dan1st3w ago
revise what?
direct_x_34
direct_x_34OP3w ago
I use UUID for id as I said before.
dan1st
dan1st3w ago
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 dator
direct_x_34
direct_x_34OP3w ago
I want to use id as UUID
dan1st
dan1st3w ago
Why do you even have the idea that this would be possible? well it might e possible if you don't use ObjectId
direct_x_34
direct_x_34OP3w ago
How can I use id as UUID string format? Is it possible to define it inside the entity?
dan1st
dan1st3w ago
?
dan1st
dan1st3w ago
Baeldung
UUID as Entity ID in MongoDB | Baeldung
Learn to use a UUID as an entity ID in MongoDB.
direct_x_34
direct_x_34OP3w ago
I also asked ChatGpt but it couldn't provide the solutions I already looked through the link before.
dan1st
dan1st3w ago
Can you try not using String for the ID? but directly using UUID? like
@Id
@Indexed(unique = true)
@Field(name = "_id")
private String UUID id = UUID.randomUUID();
@Id
@Indexed(unique = true)
@Field(name = "_id")
private String UUID id = UUID.randomUUID();
and maybe also add spring.data.mongodb.uuid-representation=standard to your application.properties
direct_x_34
direct_x_34OP3w ago
It didn't help me fix the issue
dan1st
dan1st3w ago
What happens with it? Note that old documents won't work with that so consider using a new database
direct_x_34
direct_x_34OP3w ago
No description
direct_x_34
direct_x_34OP3w ago
I already delected all collections from mongodb
dan1st
dan1st3w ago
Did you insert a new object first?
direct_x_34
direct_x_34OP3w ago
Yeah
direct_x_34
direct_x_34OP3w ago
No description
dan1st
dan1st3w ago
What does it look like in MongoDB? How did you insert that object?
direct_x_34
direct_x_34OP3w ago
I did it
No description
dan1st
dan1st3w ago
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?
direct_x_34
direct_x_34OP3w ago
Its id is 6781a12a22e4f962650b7fec. Not UUID even if I defined private String id = UUID.randomUUID().toString();
dan1st
dan1st3w ago
Can you show the code where you printed it in the Java application? before saving it and the code returning the response
direct_x_34
direct_x_34OP3w ago
id cannot perceive UUID as I said many times
dan1st
dan1st3w ago
I have no idea what you are talking about
direct_x_34
direct_x_34OP3w ago
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
dan1st
dan1st3w ago
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
ayylmao123xdd
ayylmao123xdd3w ago
🎿 🐝 🦌 @direct_x_34 is it fixed now
JavaBot
JavaBot3w ago
💤 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.
direct_x_34
direct_x_34OP3w ago
No @ayylmao123xdd
ayylmao123xdd
ayylmao123xdd3w ago
can you show the validation for this uuid thing where it gave you the 'must be a valid uuid'
ayylmao123xdd
ayylmao123xdd3w ago
but yea probably listen to daniel and read this article https://www.baeldung.com/java-mongodb-uuid
Baeldung
UUID as Entity ID in MongoDB | Baeldung
Learn to use a UUID as an entity ID in MongoDB.
dan1st
dan1st3w ago
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
ayylmao123xdd
ayylmao123xdd3w ago
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
dan1st
dan1st3w ago
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 @...
ayylmao123xdd
ayylmao123xdd3w ago
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
dan1st
dan1st3w ago
🤦
ayylmao123xdd
ayylmao123xdd3w ago
:shrugging:
dan1st
dan1st3w ago
I'm not saying anything more
ayylmao123xdd
ayylmao123xdd3w ago
disaster
JavaBot
JavaBot3w ago
💤 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.

Did you find this page helpful?