ID format issue as UUID String in Spring Boot with MongoDb
Archiviert a year ago
D
direct_x_34
Copy Paster!
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;
}
```
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"
}
]
}
```
Here is the value stored in the collection
```
_id : ObjectId(6781972fa25a3e577395c444)
AIRPORT_NAME : String
CITY_NAME : String
_class : Entity clas
```
How can I fix the issue?
